notpseudo

not

pseudo
RISC-V Assembly Reference

lyszt.net covers not and other RISC-V instructions in more detail.

Description

not inverts all the bits of a register. A 0 becomes 1, and a 1 becomes 0. The form below reads source register rs, flips all 32 bits, and writes the result to destination register rd. This operation is the bitwise complement.

not rd, rs

not is a pseudo-instruction: a readable name that the assembler rewrites into a real instruction. It becomes the instruction below, which is XOR with a mask of all 1s.

xori rd, rs, -1

XOR flips a bit wherever the mask holds a 1, so a mask of all 1s flips all bits. The value -1 is all 1s in bits, which is why it works as the mask. RISC-V has no dedicated invert instruction because this rewrite handles it.

not produces the opposite of a mask. If a pattern selects certain bits, not yields the pattern that selects the rest. Following not with and clears the bits the original mask named, which switches a set of flags off.

The RISC-V manual defines not as an assembler pseudoinstruction for xori with the constant -1, and describes that xori as a bitwise logical inversion of register rs1.

not and neg give different results. not flips bits, and neg flips the sign. not of 5 is -6, and neg of 5 is -5. The simulator's trace shows the xori that runs in place of not.

Expands to

xori rd, rs, -1

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Example

li t0, 0
not t1, t0       # t1 = -1 (every bit flipped)