Bitwise AND operation for bit masking and flag management on 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:0x16Introduced: Frontier (EVM genesis)AND performs bitwise AND on two 256-bit unsigned integers. Each bit in the result is 1 only if the corresponding bits in both operands are 1. This operation is fundamental for bit masking, extracting specific bit ranges, and checking flags.Primary uses: extracting addresses from uint256, applying bit masks, checking flag combinations.
Specification
Stack Input:
a (top)b
Stack Output:
a & b
Gas Cost: 3 (GasFastestStep)Truth Table (per bit):
a | b | a & b--|---|------0 | 0 | 00 | 1 | 01 | 0 | 01 | 1 | 1
Behavior
AND pops two values from the stack, performs bitwise AND on each corresponding bit pair, and pushes the result. The operation is:
Commutative: a & b = b & a
Associative: (a & b) & c = a & (b & c)
Identity element: a & MAX_UINT256 = a
Null element: a & 0 = 0
Examples
Basic Masking
import { and } from '@tevm/voltaire/evm/bitwise';import { createFrame } from '@tevm/voltaire/evm/Frame';// Extract lower 8 bits (least significant byte)const value = 0x123456789ABCDEFn;const mask = 0xFFn;const frame = createFrame({ stack: [value, mask] });const err = and(frame);console.log(frame.stack); // [0xEFn]
// VULNERABLE: Mask doesn't cover full addressfunction extractAddress(uint256 packed) pure returns (address) { return address(uint160(packed & 0xFFFFFFFF)); // Only 32 bits!}// CORRECT: Full 160-bit maskfunction extractAddress(uint256 packed) pure returns (address) { return address(uint160(packed & type(uint160).max));}
Flag Checking Logic Errors
// WRONG: Checks if ANY flag is set (should be AND)function hasPermission(uint256 perms, uint256 required) pure returns (bool) { return (perms | required) != 0; // Using OR instead of AND}// CORRECT: Checks if ALL required flags are setfunction hasPermission(uint256 perms, uint256 required) pure returns (bool) { return (perms & required) == required;}
Off-by-One in Bit Positions
// WRONG: Flag indices off by oneuint256 constant FLAG_0 = 1 << 1; // Should be 1 << 0uint256 constant FLAG_1 = 1 << 2; // Should be 1 << 1// CORRECT: Proper flag definitionsuint256 constant FLAG_0 = 1 << 0; // Bit 0uint256 constant FLAG_1 = 1 << 1; // Bit 1
Endianness Confusion
// Be aware: BYTE opcode uses big-endian (byte 0 = MSB)// But bitwise operations are position-agnosticbytes32 data = 0x0123456789ABCDEF;assembly { // This extracts byte 0 (0x01), not byte 31! let b := and(shr(248, data), 0xFF) // Correct}
Benchmarks
AND is one of the fastest EVM operations:Execution time (relative):
AND: 1.0x (baseline, fastest tier)
OR/XOR: 1.0x (same tier)
ADD: 1.0x (same tier)
MUL: 1.2x
DIV: 2.5x
Gas efficiency:
3 gas per 256-bit AND operation
~333,333 AND operations per million gas
Native hardware instruction on all platforms
References
Yellow Paper - Section 9.1 (Bitwise Logic Operations)