---
eip: 145
label: EIP-145
title: Bitwise shifting instructions in EVM
status: Final
type: Standards Track
category: Core
created: 2017-02-13
official: https://eips.ethereum.org/EIPS/eip-145
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md
canonical: https://eips.huatuo.xyz/eip-145/
lang: en
tldr: Added native SHL, SHR and SAR bit-shift opcodes to the EVM at 3 gas each, replacing costly multiply/divide-by-power-of-two workarounds.
tags: [evm, opcodes, core, constantinople, gas]
---

# EIP-145: Bitwise shifting instructions in EVM

> To Provide native bitwise shifting with cost on par with other arithmetic operations.

**TL;DR:** Added native SHL, SHR and SAR bit-shift opcodes to the EVM at 3 gas each, replacing costly multiply/divide-by-power-of-two workarounds.

## What problem does it solve?

The original EVM had AND, OR, XOR and NOT, but no way to shift bits. Contracts that packed several values into one 256-bit word, or decoded data byte by byte, had to fake a shift with arithmetic: `x * 2**n` for a left shift and `x / 2**n` for a right shift. The spec puts the cost of that workaround at about 35 gas, and it also meant more work for the node running it.

EIP-145 adds three real shift instructions at the cheapest gas tier.

## How it works

| Opcode | Name | Result | Notes |
| --- | --- | --- | --- |
| `0x1b` | `SHL` | `(value * 2^shift) mod 2^256` | Logical left shift |
| `0x1c` | `SHR` | `floor(value / 2^shift)` | Logical right shift, zero fill |
| `0x1d` | `SAR` | `floor(value / 2^shift)` | Arithmetic right shift, `value` is signed, sign is preserved |

Each pops two words: first the **shift amount**, then the **value**. That is the reverse of most arithmetic opcodes. It was chosen so that the value already on the stack can be shifted by pushing just the amount.

Edge cases:

- A shift of 256 or more gives `0` for `SHL` and `SHR`. For `SAR` it gives `0` for a non-negative value and `-1` (all ones) for a negative one.
- `SAR` is not the same as signed division: `SDIV(-1, 2) == 0`, but `SAR(-1, 1) == -1`, because `SAR` rounds toward negative infinity.

All three cost 3 gas (the `verylow` tier). The opcodes went live in the Constantinople upgrade ([EIP-1013](/eip-1013/)).

## Sample code

Raw EVM, shifting `1` left by `0xff` bits:

```text
PUSH32 0x0000...0001   // value
PUSH1  0xff            // shift amount (top of stack)
SHL                    // -> 0x8000...0000
```

In Solidity, `<<` and `>>` compile to these opcodes when targeting Constantinople or later:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Packing {
    // Pack two uint128 values into one storage word
    function pack(uint128 hi, uint128 lo) external pure returns (uint256) {
        return (uint256(hi) << 128) | lo;          // SHL
    }

    function unpackHi(uint256 word) external pure returns (uint128) {
        return uint128(word >> 128);               // SHR
    }

    function signedHalf(int256 x) external pure returns (int256) {
        return x >> 1;                              // SAR, rounds toward -infinity
    }
}
```

## Things to know

- Bytecode deployed before the fork is unaffected. Before Constantinople these byte values were simply invalid opcodes.
- When compiling for a pre-Constantinople EVM version, compilers fall back to `MUL`/`DIV` with `EXP`, so the cheaper opcodes depend on your target setting.
- In Solidity, `>>` on a signed integer rounds toward negative infinity, matching `SAR`. Do not assume it behaves like `/ 2` for negative numbers.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-145
