CTZW

RISC-V CTZW Instruction Details

Instruction ManualI-type (OP-IMM-32)

Count trailing zero bits in word (RV64)

Instruction Syntax

ctzw rd, rs1
Operand Breakdown
rd: x0–x31; receives a count from 0 to 32, with writes to x0 discarded.
rs1: only its low 32 bits participate; there is no immediate operand, and bits 31..20 are fixed to 011000000001.
RV64 Zbb only; scan from bit 0 toward bit 31 for the first 1, returning 32 for an all-zero low word.
rd may overlap rs1; the source is read before writeback.
ZbbBit Manipulation

Instruction Behavior

ctzw on RV64 examines only the low 32 bits of rs1 and returns a count from 0 to 32.

CTZW Decode And Execute Animation

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

rd
rs1
ctzw
,
Execution Context

This example uses fixed initial register values and PC=0x2000; instruction fetch succeeds, with no asynchronous interrupt or extra extension checks. Exception branches stop ordinary commit; post-trap PC and handler state are EEI-dependent.

t1(x6)
0x0000000000000014
31..20
19..15
14..12
11..7
6..0
011000000001
fixed imm[11:0]
00110
rs1
001
funct3
00101
rd
0011011
opcode
Execution Data Path
instruction
0x6013129B
opcode
0011011 -> OP-IMM-32
funct3
001 -> CTZW
fixed imm[11:0]
011000000001 -> CTZW selector
rd / rs1
t0(x5) / t1(x6)=0x00000014
fixed imm[11:0]
011000000001 selects CTZW (not an arithmetic immediate)
zero-bit scan
Not computed
x5
t0(x5) = 0x0000000000003001; pc = 0x0000000000002000
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: 0x6013129B
syntax : ctzw t0(x5), t1(x6)
Result: Not computed
Current architectural state
t0(x5) = 0x0000000000003001; pc = 0x0000000000002000

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

Quick Understanding & Search Notes

ctzw on RV64 examines only the low 32 bits of rs1 and returns a count from 0 to 32.

CTZW is an RV64 Zbb OP-IMM-32 instruction: it reads only rs1[31:0], and upper source bits do not participate in the count.
If the low 32 bits are zero, the result is 32.

Common Usage Scenarios

Bit Operations & Masks

Understand this scenario with real code like «ctzw x10, x11 ; x10 = trailing zeros in x11[31:0]».

Pre-Use Checklist

Syntax Check
  • rd: x0–x31; receives a count from 0 to 32, with writes to x0 discarded.
  • rs1: only its low 32 bits participate; there is no immediate operand, and bits 31..20 are fixed to 011000000001.
Semantic Check
  • RV64 Zbb only; scan from bit 0 toward bit 31 for the first 1, returning 32 for an all-zero low word.
  • rd may overlap rs1; the source is read before writeback.

Pitfalls / Common Confusions

RV64 Zbb only; only rs1[31:0] is counted, regardless of the upper 32 bits.
An all-zero low word returns 32; a set bit 0 returns 0.

FAQ

Does ctzw access memory?

It has no explicit data-memory access; it reads an integer source register and writes the count to rd.

Does ctzw interpret signed numbers?

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