Home/Instructions/Count Trailing Zeros
CTZ

RISC-V CTZ Instruction Details

Instruction ManualI-type (OP-IMM)

Count trailing zero bits

Instruction Syntax

ctz rd, rs1
Operand Breakdown
This is a I-type (OP-IMM)-format instruction. Confirm operand positions based on the assembly syntax.
ZbbBit ManipulationInteger Operation

Instruction Behavior

ctz returns the number of consecutive zero bits starting at the least-significant bit of rs1.

CTZ Decode And Execute Animation

Starts with machine-code field decoding, then shows operand reads, instruction-specific execution, and ISA-visible state update.

rd
rs1
ctz
,
Execution Context
t1(x6)
0x00000014
31..20
19..15
14..12
11..7
6..0
011000000001
fixed imm[11:0]
00110
rs1
001
funct3
00101
rd
0010011
opcode
Execution Data Path
instruction
0x60131293
opcode
0010011 -> OP-IMM
funct3
001 -> CTZ
fixed imm[11:0]
011000000001 -> CTZ selector
rd / rs1
t0(x5) / t1(x6)=0x00000014
fixed imm[11:0]
011000000001 selects CTZ (not an arithmetic immediate)
zero-bit scan
scan rs1[0] toward rs1[31] for the first 1 -> 2
x5
t0(x5) = 0x00000002
Current Step

Concept Step: receive the 32-bit instruction encoding

The machine code is split by the current instruction format; the animation starts from encoding/decode.

encoding: 0x60131293
syntax : ctz t0(x5), t1(x6)
result : t0(x5) = 0x00000002
Architectural Result
t0(x5) = 0x00000002

This animation shows ISA-visible decode and state changes, not any specific CPU pipeline, cache, prediction, or timing implementation.

Quick Understanding & Search Notes

ctz returns the number of consecutive zero bits starting at the least-significant bit of rs1.

CTZ is an OP-IMM instruction in the Zbb basic bit-manipulation extension and operates on an XLEN-wide integer register value.
If the input is zero, the result is XLEN.

Common Usage Scenarios

Bit Operations & Masks

Understand this scenario with real code like «ctz x10, x11 ; x10 = number of trailing zeros in x11».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is I-type (OP-IMM).
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

If the input is zero, the result is XLEN.

FAQ

Does ctz access memory?

No. It reads integer register operands and writes rd only.

Does ctz interpret signed numbers?

These bit operations work on bit patterns directly; aside from the specified word-width selection, arithmetic signed magnitude is not used.