Home/Instructions/AND Immediate
ANDI

RISC-V ANDI Instruction Details

Instruction ManualI-type

Bitwise AND of rs1 with sign-extended 12-bit immediate, result in rd

Instruction Syntax

andi rd, rs1, imm
Operand Breakdown
Destination rd: general-purpose register receiving the result.
Source rs1: register holding the first operand.
Immediate imm: 12-bit signed value, sign-extended before operation with rs1.
RV32ILogical

Instruction Encoding

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

ANDI uses opcode 0010011 (0x13), funct3 111. 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: 111 (0x7)

Instruction Behavior

ANDI performs bitwise AND of rs1 with the sign-extended 12-bit immediate, writing to rd. Used to clear specific bits (masking), extract bit fields, and implement modulo (when immediate is 2^n-1). The immediate is sign-extended first.

Quick Understanding & Search Notes

ANDI is a base integer bitwise logical instruction. It operates across XLEN bits; immediate forms use a sign-extended 12-bit immediate.

The I-type immediate is sign-extended to XLEN before the bitwise operation.
Integer logical operations do not raise arithmetic-overflow exceptions; rd=x0 discards the result.

Common Usage Scenarios

Address & Pointer

Understand this scenario with real code like «andi x5, x6, 0xFF # x5 = x6 & 0xFF (extract low byte)».

Bit Operations & Masks

Understand this scenario with real code like «andi x5, x6, 0xFF # x5 = x6 & 0xFF (extract low byte)».

Register Operations

Understand this scenario with real code like «andi x5, x6, 0xFF # x5 = x6 & 0xFF (extract low byte)».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is I-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

Immediate is sign-extended first
ANDI with mask of -1 is effectively a NOP (but HINT if rd=x0)

FAQ

Is the immediate zero-extended?

ANDI immediate forms use a 12-bit signed immediate that is sign-extended to XLEN.

Does it update flags?

The base integer ISA has no condition-code flags; only rd is written.