Home/Instructions/Count Trailing Zeros
CTZ

RISC-V CTZ Instruction Details

Instruction ManualR-type

Count trailing zero bits

Instruction Syntax

ctz rd, rs1
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
BZbbBit ManipulationInteger Operation

Instruction Encoding

31..20
imm[11:0]
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

CTZ uses opcode 0010011 (0x13), funct3 001, funct7 0110000. The rs1 field selects the source register, the 12-bit immediate provides the second operand, and rd selects the destination.

Format: I-type
opcode: 0010011 (0x13)
funct3: 001 (0x1)
funct7: 0110000 (0x30)

Instruction Behavior

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

Quick Understanding & Search Notes

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

These instructions are part of a B-extension subset and operate on XLEN-wide integer register values.
If the input is zero, the result is XLEN.

Common Usage Scenarios

Address & Pointer

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

Bit Operations & Masks

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

Cache & Hints

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 R-type.
  • 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.