Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

EIP-145 introduces three bitwise shift instructions in the Ethereum Virtual Machine: SHL, SHR, and SAR.

This article explains the following proposal, with additional context and examples.

You can find my other proposal articles here.

Overview

EIP-145 introduces native bitwise shift instructions to the EVM. These instructions are more efficient for clients to execute than equivalent sequences of arithmetic instructions and cost less gas when used in smart contracts.

  • Native bitwise shifts: A shift moves bits to the left or right within a word. Such operations are useful for manipulating packed data and performing numeric calculations.
  • Host execution efficiency: Dedicated EVM instructions allow clients to implement shifts directly rather than emulate them through exponentiation and arithmetic. The precise host implementation depends on the client and hardware.
  • Reduced execution cost: The instructions reduce the gas required by contracts that perform bitwise shifts. This is an EVM gas reduction, not necessarily a corresponding reduction in a cloud provider's charges.

Motivation

Before EIP-145, the EVM provided arithmetic and logical operations but lacked dedicated bitwise shift instructions.

A shift could be emulated using arithmetic operations, but this required more instructions and gas. The proposal compares arithmetic implementations of left and logical right shifts, costing 35 gas each, with the new dedicated shift instructions, costing just 3 gas each.

This reduces the cost of bit manipulation and makes shift-heavy contract code more efficient.

Specification

SHL: 0x1b

SHL performs a logical left shift. It pops two values from the stack: arg1, the shift amount, followed by arg2, the value to shift.

It shifts arg2 to the left by arg1 bits, filling vacated low-order positions with zeros.

(arg2∗2arg1)mod2256(arg2 * 2^{arg1}) mod 2^{256}

Mathematically, the result is arg2 multiplied by two raised to arg1, reduced modulo 2 to the power of 256. This keeps the result within the EVM's 256-bit word size.

If arg1 is 256 or greater, the result is zero because all original bits have been shifted out of the word.

The proposal describes this operation as equivalent to PUSH1 2 EXP MUL. The native instruction performs the shift directly, making bit manipulation more efficient.

SHR: 0x1c

SHR performs a logical right shift. It pops arg1 as the shift amount and arg2 as the value to shift.

The bits of arg2 move to the right by arg1 positions, with zeros filling the vacated high-order positions.

The result is the floor of the unsigned value arg2 divided by two raised to arg1. If arg1 is at least 256, the result is zero.

The proposal describes this operation as equivalent to PUSH1 2 EXP DIV. SHR is useful for extracting packed fields and performing unsigned bitwise computations.

SAR: 0x1d

SAR performs an arithmetic right shift. Unlike SHR, it interprets arg2 as a signed 256-bit two's-complement integer and fills the vacated high-order bits with the original sign bit.

It pops arg1 as the shift amount and arg2 as the value to shift. The mathematical result corresponds to signed division by two raised to arg1, rounded toward negative infinity.

For example, shifting -3 right by one bit produces -2, not -1. Shifting -1 by one bit produces -1. The original article's example incorrectly generalizes the latter result to all negative numbers.

If arg1 is at least 256, SAR returns zero for nonnegative inputs and -1 for negative inputs.

SAR is not equivalent to PUSH1 2 EXP SDIV: SDIV rounds toward zero, whereas SAR rounds toward negative infinity. This distinction matters when shifting negative values that are not evenly divisible by the corresponding power of two.

All three shift opcodes belong to the very-low gas-cost tier and cost 3 gas each.

For more background on shift opcodes, see:

Rationale

The operand order is deliberately chosen to make shifting a value already on the stack convenient.

The first value popped is the shift amount, and the second is the value being shifted. A program can therefore push the shift amount on top of an existing value and execute the shift without rearranging the stack.

Unlike the shift instructions, ordinary arithmetic instructions such as DIV interpret their operands according to their own stack conventions. ADD is commutative, so its result does not illustrate a meaningful difference in operand order.

Test cases

SHL

Tests
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x00
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x01
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000002
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0xff
SHL
---
0x8000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x0100
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x0101
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x00
SHL
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x01
SHL
---
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xff
SHL
---
0x8000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x0100
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000000
PUSH 0x01
SHL
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x01
SHL
---
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe

These test cases illustrate logical left shifts.

  • A shift by zero leaves the input unchanged. For example, shifting 1 left by zero bits returns 1.
  • Shifting 1 left by one bit returns 2.
  • Shifting 1 left by 255 bits sets only the most significant bit, representing the unsigned value 2 to the power of 255.
  • Shifting any value by at least 256 bits returns zero.
  • Shifting an all-ones word left fills its low-order positions with zeros and discards bits shifted beyond the 256-bit boundary.
  • Shifting zero returns zero. Shifting the maximum positive signed 256-bit value left by one bit produces an all-ones word except for the least significant bit.

The test vectors demonstrate the fixed-width behavior of SHL, including truncation at the word boundary.

SHR

Tests
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x00
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x01
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x01
SHR
---
0x4000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0xff
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x0100
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x0101
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000000
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x00
SHR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x01
SHR
---
0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xff
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x0100
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000000
PUSH 0x01
SHR
---
0x0000000000000000000000000000000000000000000000000000000000000000

These test cases illustrate logical right shifts.

  • A shift by zero leaves the input unchanged.
  • Shifting 1 right by one bit returns zero because the operation discards the fractional part of unsigned division.
  • Shifting a word with only the most significant bit set right by one bit moves that bit to the second-most significant position.
  • Shifting that same word by 255 bits returns 1.
  • Shifting any value by at least 256 bits returns zero.
  • Shifting an all-ones word right fills its high-order positions with zeros. For example, a one-bit shift clears the most significant bit while leaving the other bits set.
  • Shifting zero returns zero.

Unlike arithmetic right shift, SHR always inserts zeros regardless of the original sign bit.

SAR

Tests
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x00
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000001
PUSH 0x01
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x01
SAR
---
0xc000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0xff
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x0100
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0x8000000000000000000000000000000000000000000000000000000000000000
PUSH 0x0101
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x00
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x01
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xff
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x0100
SAR
---
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
text
PUSH 0x0000000000000000000000000000000000000000000000000000000000000000
PUSH 0x01
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x4000000000000000000000000000000000000000000000000000000000000000
PUSH 0xfe
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xf8
SAR
---
0x000000000000000000000000000000000000000000000000000000000000007f
text
PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xfe
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000001
text
PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0xff
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000000
text
PUSH 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
PUSH 0x0100
SAR
---
0x0000000000000000000000000000000000000000000000000000000000000000

These test cases illustrate arithmetic right shifts.

  • A shift by zero leaves the input unchanged.
  • Shifting a positive number right by one bit is equivalent to dividing it by two and rounding down.
  • For negative inputs, the high-order positions are filled with ones, preserving the sign.
  • A shift by at least 256 bits returns -1 for negative inputs and zero for nonnegative inputs.
  • An all-ones word represents -1 in two's-complement notation and remains unchanged under any arithmetic right shift. It is not the minimum signed value, which is represented by a word with only its most significant bit set.
  • Shifting zero returns zero.
  • Shifting a positive word progressively removes low-order bits, eventually producing zero.

SAR is useful when manipulating signed integers. Together, these test cases illustrate how its sign extension and rounding behavior differ from SHR and SDIV.

Implementation

Client support

Compiler support

These links refer to historical implementation work associated with the proposal.

Tests

Sources

https://github.com/ethereum/tests/tree/develop/src/GeneralStateTestsFiller/stShift

Filled Tests

https://github.com/ethereum/tests/tree/develop/GeneralStateTests/stShift https://github.com/ethereum/tests/tree/develop/BlockchainTests/GeneralStateTests/stShift

Citation

Alex Beregszaszi (@axic), Paweł Bylica (@chfast), "EIP-145: Bitwise shifting instructions in EVM," Ethereum Improvement Proposals, no. 145, February 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-145.

Conclusion

EIP-145 introduces efficient 256-bit shift instructions for the EVM. SHL performs logical left shifts, SHR performs logical right shifts, and SAR performs arithmetic right shifts with sign extension. The dedicated opcodes reduce the gas cost of operations that previously required sequences of arithmetic instructions.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

You can also find my work on the following sites.

https://chaldene.net/