Overview
Opcode:0x1d
Introduced: Constantinople (EIP-145)
SAR performs arithmetic (signed) shift right on a 256-bit value interpreted as a two’s complement signed integer. Vacated bits (on the left) are filled with the sign bit (MSB), preserving the sign of the value. This operation efficiently divides signed integers by powers of 2 with correct rounding toward negative infinity.
Before EIP-145, signed right shifts required expensive SDIV + EXP operations. SAR reduces this to 3 gas.
Note: SAR is for signed values. For unsigned division, use SHR (0x1c).
Specification
Stack Input:Behavior
SAR pops two values from the stack:- shift - number of bit positions to shift right (0-255)
- value - 256-bit value interpreted as signed i256 (two’s complement)
- If MSB = 0 (positive): fill with 0s (same as SHR)
- If MSB = 1 (negative): fill with 1s (preserve negative sign)
- shift >= 256 and positive: result = 0
- shift >= 256 and negative: result = -1 (0xFFFF…FFFF)
Examples
Positive Value (Same as SHR)
Negative Value (Sign Extension)
Divide Negative by Power of 2
Maximum Shift on Negative
Maximum Shift on Positive
SAR vs SHR Comparison
Gas Cost
Cost: 3 gas (GasFastestStep) Pre-EIP-145 equivalent:Edge Cases
Zero Value
Zero Shift
Minus One
Minimum Signed Int
Maximum Positive Int
Shift to Sign Bit Only
Edge of Sign Change
Hardfork Check
Stack Underflow
Out of Gas
Common Usage
Signed Division by Power of 2
Extract Signed Value from Packed Data
Fixed-Point Arithmetic
Sign Extension
Check Sign of Packed Int
Implementation
- TypeScript
Testing
Test Coverage
Edge Cases Tested
- Positive value shifts (same as SHR)
- Negative value sign extension
- Signed division by powers of 2
- Shift >= 256 on positive (→ 0)
- Shift >= 256 on negative (→ -1)
- Zero shift (identity)
- -1 shifted remains -1
- MIN_INT256 handling
- MAX_INT256 handling
- Comparison with SHR
- Hardfork compatibility
- Stack underflow
- Out of gas
Security
SHR vs SAR Confusion
Rounding Direction
Sign Extension Pitfalls
Mixed Signed/Unsigned Operations
Benchmarks
SAR is one of the fastest EVM operations: Execution time (relative):- SAR: 1.0x (baseline, fastest tier)
- SHR/SHL: 1.0x (same tier)
- SDIV: 2.5x
Gas savings: 2-1612 gas per signed shift vs pre-EIP-145 methods.
References
- EIP-145 - Bitwise shifting instructions (SHL, SHR, SAR)
- Yellow Paper - Section 9.4.1
- EVM Codes - SAR
- Two’s Complement - Signed integer representation
Related Documentation
- SHR (0x1c) - Logical shift right (unsigned)
- SHL (0x1b) - Shift left
- SDIV (0x05) - Signed division
- SIGNEXTEND (0x0b) - Sign extension
- Hardfork - Constantinople

