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 Behavior

ANDI uses the I-type format. It ANDs rs1 with a 12-bit immediate sign-extended to XLEN and writes rd. It can retain or clear bits, or extract fields, with an encodable mask; mask use must account for sign extension.

ANDI Decode And Execute Animation

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

rd
rs1
imm
andi
,
,
Execution Context
t1(x6)
0x00000014
31..20
19..15
14..12
11..7
6..0
000011111111
imm[11:0]
00110
rs1
111
funct3
00101
rd
0010011
opcode
Execution Data Path
instruction
0x0FF37293
opcode
0010011 -> OP-IMM
funct3
111 -> ANDI bitwise AND
rd / rs1
t0(x5) / t1(x6)
imm[11:0]
000011111111 -> sign_extend(255)=0x000000FF
bitwise ALU
0x00000014 & 0x000000FF = 0x00000014
x5
t0(x5) = 0x00000014
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: 0x0FF37293
syntax : andi t0(x5), t1(x6), 255
result : t0(x5) = 0x00000014
Architectural Result
t0(x5) = 0x00000014
Bit-by-Bit Logic View
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
rs1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
sign-extended imm
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
&
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

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

Quick Understanding & Search Notes

ANDI is an RV32I/RV64I OP-IMM logical-immediate instruction: imm[11:0] is sign-extended to XLEN, ANDed bit by bit with rs1, and written to rd. It is useful for masks that fit the signed 12-bit immediate.

opcode=0010011 selects OP-IMM, and funct3=111 selects ANDI.
The I-type imm[11:0] is sign-extended to XLEN; an all-ones encoded immediate represents -1, not zero-extended 4095.

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
  • Verify the immediate field is within the valid range.
  • Confirm source register rs1 points to the correct operand.
Semantic Check
  • Check if the immediate sign-extension matches expectations.
  • Ensure the result register rd has a clear purpose.

Pitfalls / Common Confusions

Immediate is sign-extended first
imm=-1 sign-extends to all ones; the result copies rs1's bit pattern to rd, so it is not state-preserving when rd differs from rs1.

FAQ

Is the immediate zero-extended?

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

Can andi encode any 32-bit mask?

No. ANDI has only a signed 12-bit immediate field; larger masks usually need to be built in a register and then used with AND.