Bitwise NOT operation (one’s complement) for inverting all bits in 256-bit values
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x19Introduced: Frontier (EVM genesis)NOT performs bitwise NOT (one’s complement) on a 256-bit unsigned integer, inverting every bit (0 becomes 1, 1 becomes 0). This operation is fundamental for bit masking, creating inverted patterns, and logical negation in bitwise contexts.Primary uses: creating inverse masks, clearing bits (NOT + AND), two’s complement conversion (NOT + 1).
Specification
Stack Input:
a (top)
Stack Output:
~a
Gas Cost: 3 (GasFastestStep)Truth Table (per bit):
a | ~a--|----0 | 11 | 0
Behavior
NOT pops one value from the stack, inverts all 256 bits, and pushes the result. The operation is:
Unary: operates on single operand
Involutory: ~~a = a (double negation returns original)
Self-inverse: a XOR (~a) = MAX_UINT256
Result: ~a = MAX_UINT256 - a for unsigned interpretation.
Examples
Basic Inversion
import { not } from '@tevm/voltaire/evm/bitwise';import { createFrame } from '@tevm/voltaire/evm/Frame';// Invert bitsconst value = 0b11001100n;const frame = createFrame({ stack: [value] });const err = not(frame);// All 256 bits inverted (showing lower byte)const MAX = (1n << 256n) - 1n;console.log(frame.stack[0] & 0xFFn); // 0b00110011
Create Inverse Mask
// Create mask to clear specific bitsconst setBits = 0xFFn; // Lower 8 bitsconst frame = createFrame({ stack: [setBits] });not(frame);const clearMask = frame.stack[0];// clearMask = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00// Use with AND to clear lower 8 bits
Zero to Max
// NOT of zero is MAX_UINT256const frame = createFrame({ stack: [0n] });not(frame);const MAX = (1n << 256n) - 1n;console.log(frame.stack[0] === MAX); // true
Max to Zero
// NOT of MAX_UINT256 is zeroconst MAX = (1n << 256n) - 1n;const frame = createFrame({ stack: [MAX] });not(frame);console.log(frame.stack[0]); // 0n
// NOT with single bit set (bit 0)const singleBit = 1n;const frame = createFrame({ stack: [singleBit] });not(frame);// Result: all bits except bit 0 are setconst expected = ((1n << 256n) - 1n) - 1n;console.log(frame.stack[0] === expected); // true
// Invert all flags in bitmapmapping(uint256 => uint256) public bitmap;function invertBitmap(uint256 bucket) internal { bitmap[bucket] = ~bitmap[bucket];}
Complement in Range Checks
// Check if value is NOT in set (using bitmap)function isNotMember(uint256 value, uint256 bitmap) pure returns (bool) { uint256 mask = 1 << (value % 256); return (bitmap & mask) == 0; // Equivalent to: (~bitmap & mask) != 0}
Implementation
TypeScript
/** * NOT opcode (0x19) - Bitwise NOT operation (one's complement) */export function op_not(frame: FrameType): EvmError | null { // Consume gas (GasFastestStep = 3) frame.gasRemaining -= 3n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Pop operand if (frame.stack.length < 1) return { type: "StackUnderflow" }; const a = frame.stack.pop(); // Compute bitwise NOT (one's complement) // Note: In JavaScript, ~a only works for 32-bit integers // For 256-bit: use MAX_UINT256 XOR or subtraction const MAX_UINT256 = (1n << 256n) - 1n; const result = a ^ MAX_UINT256; // Equivalent to ~a for 256-bit // Push result if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(result); // Increment PC frame.pc += 1; return null;}
// WRONG: NOT alone doesn't negate signed integersfunction negate(int256 x) pure returns (int256) { return int256(~uint256(x)); // Missing +1 for two's complement!}// CORRECT: Two's complement requires NOT + 1function negate(int256 x) pure returns (int256) { return int256(~uint256(x) + 1); // -x = ~x + 1}
Mask Creation Errors
// DANGEROUS: Creating masks without boundary checksfunction clearLowerBits(uint256 value, uint256 numBits) pure returns (uint256) { require(numBits <= 256, "invalid bit count"); uint256 mask = (1 << numBits) - 1; return value & ~mask;}// Risk: numBits > 256 causes mask overflow
Incorrect Bit Clearing
// WRONG: Using NOT alone (clears all OTHER bits)function clearFlag(uint256 flags, uint256 flag) pure returns (uint256) { return ~flag; // Returns inverted flag, not modified flags!}// CORRECT: NOT + AND to clear specific bitsfunction clearFlag(uint256 flags, uint256 flag) pure returns (uint256) { return flags & ~flag;}
Signed vs Unsigned
// PITFALL: NOT on unsigned vs signed interpretationuint256 unsigned_val = 5;int256 signed_val = 5;// NOT on unsigned: MAX_UINT256 - 5uint256 unsigned_result = ~unsigned_val; // Very large positive number// Two's complement on signed: -6 (not -5!)int256 signed_result = int256(~uint256(signed_val)); // -6, not -5int256 proper_negation = -signed_val; // -5 (correct)
Benchmarks
NOT is one of the fastest EVM operations:Execution time (relative):
NOT: 1.0x (baseline, fastest tier)
AND/OR/XOR: 1.0x (same tier)
ADD: 1.0x (same tier)
MUL: 1.2x
DIV: 2.5x
Gas efficiency:
3 gas per 256-bit NOT operation
~333,333 NOT operations per million gas
Native hardware instruction on all platforms
References
Yellow Paper - Section 9.1 (Bitwise Logic Operations)