Home/Instructions/AND with Complement
ANDN

RISC-V ANDN Instruction Details

Instruction ManualR-type

AND with inverted operand: rd = rs1 & ~rs2

Instruction Syntax

andn rd, rs1, rs2

rd and rs1 select x0–x31; sources are read before writeback, and x0 writes are discarded.

rs2 is the second source, inverted across XLEN bits; rs1 is not inverted.

Requires Zbb or Zbkb on RV32/RV64; rd = rs1 & ~rs2.

BZbbZbkbBit ManipulationInteger Operation

Instruction Behavior

With Zbb or Zbkb, andn computes rd = rs1 & ~rs2 across XLEN bits.

ANDN Decode And Execute Animation

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

rd
rs1
rs2
andn
,
,
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.

a0(x10)
0x00001000
t1(x6)
0x00000014
31..25
24..20
19..15
14..12
11..7
6..0
0100000
funct7
00110
rs2
01010
rs1
111
funct3
00101
rd
0110011
opcode
Execution Data Path
instruction
0x406572B3
opcode
0110011 -> OP
funct3
111 -> ANDN
funct7
0100000 -> ANDN selector
rd / rs1 / rs2
t0(x5) / a0(x10) / t1(x6)
bitwise ALU
Not computed
x5
t0(x5) = 0x00003001; pc = 0x00002000
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: 0x406572B3
syntax : andn t0(x5), a0(x10), t1(x6)
Pending result: Not computed
Current architectural state
t0(x5) = 0x00003001; pc = 0x00002000
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
1
0
0
0
0
0
0
0
0
0
0
0
0
rs1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
0
1
1
rs2 (inverted)
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
& ~
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

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

Quick Understanding & Search Notes

andn computes rs1 & ~rs2: it inverts rs2 bit by bit, then ANDs it with rs1.

andn belongs to Zbb and is also included in Zbkb. It operates on XLEN-wide integer-register bit patterns.
rs2 is inverted before ANDing with rs1; do not read it as ~rs1 & rs2.

Common Usage Scenarios

Bit Operations & Masks

RV32/RV64 with Zbb or Zbkb; x11=0xff and x12=0x0f: x10=0xf0, clearing bits selected by the mask.

Register Operations

RV32/RV64 with Zbb or Zbkb; x11=0xff and x12=0x0f: x10=0xf0, clearing bits selected by the mask.

Pre-Use Checklist

Syntax Check
  • rd and rs1 select x0–x31; sources are read before writeback, and x0 writes are discarded.
  • rs2 is the second source, inverted across XLEN bits; rs1 is not inverted.
Semantic Check
  • Requires Zbb or Zbkb on RV32/RV64; rd = rs1 & ~rs2.

Pitfalls / Common Confusions

rs2 is inverted before ANDing with rs1; do not read it as ~rs1 & rs2.