Overview
Bitwise operations provide low-level bit manipulation on 256-bit (32-byte) values. These operations enable efficient masking, flag management, and bit-level data packing critical for optimized smart contract implementations. 8 opcodes enable:- Logical operations: AND, OR, XOR, NOT
- Byte extraction: BYTE
- Shift operations (EIP-145): SHL, SHR, SAR
Opcodes
| Opcode | Name | Gas | Stack In → Out | Description |
|---|---|---|---|---|
| 0x16 | AND | 3 | a, b → a&b | Bitwise AND |
| 0x17 | OR | 3 | a, b → a|b | Bitwise OR |
| 0x18 | XOR | 3 | a, b → a^b | Bitwise XOR |
| 0x19 | NOT | 3 | a → ~a | Bitwise NOT (one’s complement) |
| 0x1a | BYTE | 3 | i, x → x[i] | Extract byte at index i |
| 0x1b | SHL | 3 | shift, val → val<<shift | Shift left (Constantinople+) |
| 0x1c | SHR | 3 | shift, val → val>>shift | Logical shift right (Constantinople+) |
| 0x1d | SAR | 3 | shift, val → val>>shift | Arithmetic shift right (Constantinople+) |
Bit Manipulation Patterns
Masking
Extract specific bits using AND:Flag Management
Use individual bits as boolean flags:Data Packing
Pack multiple values into single uint256:Shift Operations (EIP-145)
EIP-145 Background
Before Constantinople (pre-EIP-145), shift operations required expensive arithmetic:- Left shift:
value * 2^shift(MUL + EXP) - Right shift:
value / 2^shift(DIV + EXP)
Shift Direction
Stack order matters:Logical vs Arithmetic Shifts
SHR (Logical Shift Right):- Shifts bits right, filling with zeros
- Unsigned operation
- Divides by powers of 2
- Shifts bits right, preserving sign bit
- Signed operation (two’s complement)
- Divides signed integers by powers of 2
Overflow Behavior
Shifts >= 256 bits have defined behavior:Common Patterns
Efficient Multiplication/Division by Powers of 2
Extract Address from uint256
Check if Power of 2
Count Set Bits (Hamming Weight)
Bit Reversal
Zero/One Extension
Gas Costs
All bitwise operations cost 3 gas (GasFastestStep):| Category | Gas | Opcodes |
|---|---|---|
| Very Low (Fastest Step) | 3 | AND, OR, XOR, NOT, BYTE, SHL, SHR, SAR |
- Bitwise ops: 3 gas
- ADD/SUB: 3 gas
- MUL/DIV/MOD: 5 gas
- Shifts replace expensive MUL/EXP or DIV/EXP combinations (5-60+ gas → 3 gas)
Edge Cases
Maximum Values
Zero Inputs
Byte Extraction
Shift Edge Cases
Implementation
TypeScript
Zig
Security Considerations
Off-by-One Errors
Bit indexing is zero-based and left-to-right (MSB to LSB):Mask Construction
Incorrect masks can leak unintended bits:Shift Amount Validation
Sign Extension Pitfalls
SAR treats MSB as sign bit. Ensure values are properly signed:Benchmarks
Bitwise operations are among the fastest EVM operations:| Operation | Gas | Relative Speed |
|---|---|---|
| AND/OR/XOR/NOT | 3 | Fastest |
| BYTE | 3 | Fastest |
| SHL/SHR/SAR | 3 | Fastest (vs 5-60+ pre-EIP-145) |
- Pre-Constantinople: Left shift = MUL + EXP = 5 + (10 + 50/byte) gas
- Post-Constantinople: SHL = 3 gas
- Savings: 12-1607 gas per shift operation
References
- Yellow Paper - Section 9.4.1 (Arithmetic Operations)
- EIP-145 - Bitwise shifting instructions (SHL, SHR, SAR)
- evm.codes - Interactive reference
- Hacker’s Delight - Bit manipulation techniques
Related Documentation
- Arithmetic Operations - ADD, MUL, DIV, MOD
- Comparison Operations - LT, GT, EQ
- Gas Constants - Gas cost definitions
- Hardfork - Constantinople (EIP-145)

