Bitwise OR operation for combining flags and setting bits 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:0x17Introduced: Frontier (EVM genesis)OR performs bitwise OR on two 256-bit unsigned integers. Each bit in the result is 1 if either (or both) corresponding bits in the operands are 1. This operation is fundamental for combining flags, setting specific bits, and data packing.Primary uses: enabling multiple flags, setting bits in bitmaps, combining packed data fields.
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 | 11 | 0 | 11 | 1 | 1
Behavior
OR pops two values from the stack, performs bitwise OR 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 | 0 = a
Null element: a | MAX_UINT256 = MAX_UINT256
Examples
Set Multiple Flags
import { or } from '@tevm/voltaire/evm/bitwise';import { createFrame } from '@tevm/voltaire/evm/Frame';// Enable flags at bits 0 and 2const existing = 0b0000n;const flags = 0b0101n; // Bits 0 and 2const frame = createFrame({ stack: [existing, flags] });const err = or(frame);console.log(frame.stack[0].toString(2)); // '101' (0b0101)
// Set bit 5 in existing valueconst value = 0b00001000n; // Bit 3 is setconst setBit5 = 0b00100000n; // Bit 5 maskconst frame = createFrame({ stack: [value, setBit5] });or(frame);console.log(frame.stack[0].toString(2)); // '101000' (bits 3 and 5)
Commutative Property
// a | b = b | aconst a = 0b1100n;const b = 0b1010n;const frame1 = createFrame({ stack: [a, b] });or(frame1);const frame2 = createFrame({ stack: [b, a] });or(frame2);console.log(frame1.stack[0] === frame2.stack[0]); // true (both 0b1110)
Gas Cost
Cost: 3 gas (GasFastestStep)OR shares the lowest gas tier with:
AND (0x16), XOR (0x18), NOT (0x19)
BYTE (0x1a)
SHL (0x1b), SHR (0x1c), SAR (0x1d)
ADD (0x01), SUB (0x03)
Comparison operations
Edge Cases
Identity Element
// OR with zeroconst value = 0x123456n;const frame = createFrame({ stack: [value, 0n] });or(frame);console.log(frame.stack[0] === value); // true (identity)
Null Element
// OR with all onesconst MAX = (1n << 256n) - 1n;const value = 0x123456n;const frame = createFrame({ stack: [value, MAX] });or(frame);console.log(frame.stack[0] === MAX); // true
Self OR
// a | a = a (idempotent)const value = 0x123456n;const frame = createFrame({ stack: [value, value] });or(frame);console.log(frame.stack[0] === value); // true
Alternating Bits
// Complementary patterns OR to all onesconst pattern1 = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn;const pattern2 = 0x5555555555555555555555555555555555555555555555555555555555555555n;const frame = createFrame({ stack: [pattern1, pattern2] });or(frame);const MAX = (1n << 256n) - 1n;console.log(frame.stack[0] === MAX); // true (all bits set)
// Grant READ and WRITE permissionsuint256 constant READ = 1 << 0;uint256 constant WRITE = 1 << 1;uint256 constant EXECUTE = 1 << 2;function grantPermissions(uint256 current) pure returns (uint256) { return current | READ | WRITE; // Enable both flags}
Set Bits in Bitmap
// Mark slots as occupied in storage bitmapmapping(uint256 => uint256) public bitmap;function markOccupied(uint256 index) internal { uint256 bucket = index / 256; uint256 bit = index % 256; bitmap[bucket] |= (1 << bit); // Set bit}
// WRONG: Using AND instead of OR to set flagsfunction addPermission(uint256 perms, uint256 flag) pure returns (uint256) { return perms & flag; // Removes all other flags!}// CORRECT: Use OR to preserve existing flagsfunction addPermission(uint256 perms, uint256 flag) pure returns (uint256) { return perms | flag;}
Overlapping Bit Positions
// DANGEROUS: Flag definitions overlapuint256 constant FLAG_A = 1 << 0; // Bit 0uint256 constant FLAG_B = 1 << 0; // Also bit 0! (collision)// SAFE: Unique bit positionsuint256 constant FLAG_A = 1 << 0; // Bit 0uint256 constant FLAG_B = 1 << 1; // Bit 1uint256 constant FLAG_C = 1 << 2; // Bit 2
Unintended Side Effects
// DANGEROUS: OR can never clear bits, only set themfunction updateFlags(uint256 current, uint256 desired) pure returns (uint256) { return current | desired; // Can't remove flags!}// BETTER: Explicit set/clear interfacefunction setFlags(uint256 current, uint256 flags) pure returns (uint256) { return current | flags;}function clearFlags(uint256 current, uint256 flags) pure returns (uint256) { return current & ~flags;}
Packed Data Corruption
// VULNERABLE: OR can corrupt existing packed fieldsstruct Packed { uint160 addr; // Bits 0-159 uint96 value; // Bits 160-255}// Wrong: OR overwrites existing addressfunction updateValue(uint256 packed, uint96 newValue) pure returns (uint256) { return packed | (uint256(newValue) << 160); // Address corrupted if newValue has lower bits!}// Correct: Clear field first, then ORfunction updateValue(uint256 packed, uint96 newValue) pure returns (uint256) { uint256 addrMask = (1 << 160) - 1; uint256 addr = packed & addrMask; // Extract address return addr | (uint256(newValue) << 160); // Recombine}
Benchmarks
OR is one of the fastest EVM operations:Execution time (relative):
OR: 1.0x (baseline, fastest tier)
AND/XOR: 1.0x (same tier)
ADD: 1.0x (same tier)
MUL: 1.2x
DIV: 2.5x
Gas efficiency:
3 gas per 256-bit OR operation
~333,333 OR operations per million gas
Native hardware instruction on all platforms
References
Yellow Paper - Section 9.1 (Bitwise Logic Operations)