Home/Instructions/Count Leading Zeros
CLZ

RISC-V CLZ Instruction Details

Instruction ManualR-type

Count leading zero bits

Instruction Syntax

clz 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

CLZ 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

clz returns the number of consecutive zero bits starting at the most-significant bit of rs1.

Quick Understanding & Search Notes

clz returns the number of consecutive zero bits starting at the most-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

Bit Operations & Masks

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

Floating Point Basic

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

Numerical Computing

Understand this scenario with real code like «clz x10, x11 ; x10 = number of leading 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 clz access memory?

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

Does clz interpret signed numbers?

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