EIPs Explained

EIP-145 · Final · Core · 2017-02-13

EIP-145: Bitwise shifting instructions in EVM

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).

Sample code

Raw EVM, shifting 1 left by 0xff bits:

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:

// 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.

#evm #opcodes #core #constantinople #gas

Details

Authors
Alex Beregszaszi (@axic), Paweł Bylica (@chfast)
Status
Final
Type
Standards Track / Core
Created
2017-02-13
Required by

Machine-readable: Markdown · JSON