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
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..25
funct7
24..20
rs2
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

ANDN uses opcode 0110011 (0x33), funct3 111, funct7 0100000. The rs1 and rs2 fields select the two source registers, and rd selects the destination register.

Format: R-type
opcode: 0110011 (0x33)
funct3: 111 (0x7)
funct7: 0100000 (0x20)

Instruction Behavior

andn computes rs1 & ~rs2, commonly used to clear mask bits selected by rs2.

Quick Understanding & Search Notes

andn computes rs1 & ~rs2, commonly used to clear mask bits selected by rs2.

These instructions are part of a B-extension subset and operate on XLEN-wide integer register values.
rs2 is inverted before ANDing with rs1; do not read it as ~rs1 & rs2.

Common Usage Scenarios

Bit Operations & Masks

Understand this scenario with real code like «andn x10, x11, x12 ; x10 = x11 & ~x12».

Crypto & Security

Understand this scenario with real code like «andn x10, x11, x12 ; x10 = x11 & ~x12».

Register Operations

Understand this scenario with real code like «andn x10, x11, x12 ; x10 = x11 & ~x12».

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

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

FAQ

Does andn access memory?

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

Does andn interpret signed numbers?

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