Skip to main content
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: 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:
Stack Output:
Gas Cost: 3 (GasFastestStep) Hardfork: Constantinople (EIP-145) or later Shift Behavior:

Behavior

SAR pops two values from the stack:
  1. shift - number of bit positions to shift right (0-255)
  2. value - 256-bit value interpreted as signed i256 (two’s complement)
Result is value shifted right by shift positions, with the sign bit (MSB) replicated to fill vacated high-order bits. Sign Extension:
  • If MSB = 0 (positive): fill with 0s (same as SHR)
  • If MSB = 1 (negative): fill with 1s (preserve negative sign)
Overflow behavior:
  • 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:
Savings: 12-1612 gas per signed shift operation.

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

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 comparison (signed right shift by 3): Gas savings: 2-1612 gas per signed shift vs pre-EIP-145 methods.

References