30.9.2-9Bitmanip II: andn — bset (Reference)

Part III Linux boot: recommended Vol. I (Unprivileged) pp. 229–236 · ~3 min read

The alphabetical reference begins: the logical-with-negate flagship and the seven Zbs single-bit operations. All are one-cycle R-type (or OP-IMM shamt-type) instructions.

andn through bset (OP opcode unless noted; single-bit index = rs2/shamt mod XLEN)
Semanticsfunct7 / funct3In
andn rd, rs1, rs2rd = rs1 & ~rs2 — the mask-off idiom without a NOT.0100000 / 111Zbb, Zbkb
bclr rd, rs1, rs2rd = rs1 & ~(1 << (rs2 mod XLEN)) — clear one bit by index.0100100 / 001Zbs
bclri rd, rs1, shamtImmediate-index clear.OP-IMM, 0100100 / 001Zbs
bext rd, rs1, rs2rd = (rs1 >> (rs2 mod XLEN)) & 1 — EXTRACT the bit to position 0. The single-bit test.0100100 / 101Zbs
bexti rd, rs1, shamtImmediate-index extract.OP-IMM, 0100100 / 101Zbs
binv rd, rs1, rs2rd = rs1 ^ (1 << (rs2 mod XLEN)) — flip one bit.0110100 / 001Zbs
binvi rd, rs1, shamtImmediate-index invert.OP-IMM, 0110100 / 001Zbs
bset rd, rs1, rs2rd = rs1 | (1 << (rs2 mod XLEN)) — set one bit.0010100 / 001Zbs
Dotted-underlined cells have explanations — click one.
01001003125rs2 / shamt2420rs11915001 / 1011412rd117011001160The Zbs shape: bclr (funct7 pattern 0x10100, funct3 selects op)
Click a field for its role.

Idioms these erase: andn kills the not t0, rs2; and rd, rs1, t0 pair everywhere a mask is applied inverted (branch-free selects, bitboard code); the Zbs quartet replaces li t0, 1; sll t0, t0, idx; op three-instruction sequences for every dynamic flag operation — and bexti is the classic “test bit n” in one instruction with the result ready for a branch.

Hardware Designer Notes

Everything on this page is decode plus a handful of gates on paths you already own. The single-bit one-hot decoder is the only new structure, and it’s a 6-to-64 decoder you likely have in the shifter already.

Minimal Linux-boot hart MUST

  • Route the rs2-inverter (shared with the subtractor per the spec’s own hint) for andn/orn/xnor
  • Build the one-hot decoder (1 << index-mod-XLEN) once and share it across bclr/binv/bset; bext reuses the existing shifter’s LSB tap

MAY simplify / trap-and-emulate

  • Fuse bexti + beqz/bnez into a test-bit-and-branch macro-op — the dominant use pattern

Check yourself — andn & single-bit ops

1.How does bext rd, rs1, rs2 differ from bclr?

2.Why does the RV64 encoding of bclri/bexti/binvi/bseti eat one bit of funct7?

3.What hardware does andn (and orn/xnor) actually cost?

3 questions