In this track: Inside the EVM
advanced · 4 MIN READ

Shifts and byte extraction

Shift direction and byte numbering must be stated explicitly.

LET’S PICTURE IT

Moving a row of boxes is different from choosing one box by its printed index.

SEE IT FOR YOURSELF
Watch a stack machine execute a tiny programStep 1 / 4
Watch a stack machine execute a tiny programProgram→ PUSH1 0x02PUSH1 0x03ADD · STOPStackEmptyPC = 0A fixed local model; not a complete EVM implementationPUSH1 0x02Push immediate 2Next PC = 2Stack · top first2PUSH1 occupies one opcode byte plus one immediate bytePUSH1 0x03Push immediate 3Next PC = 4Stack · top first32The first value is below the most recently pushed valueADD3 + 2 = 5Next: STOP at PC 5Stack5No automatic SSTOREStack, memory and persistent storage are different locations

The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Distinguish logical directions

SHL shifts bits left, discarding overflow beyond the word. SHR shifts right and fills high positions with zeros.

Recognize arithmetic right shift

SAR preserves the signed interpretation by filling high bits according to the sign. It differs from SHR for words with the high bit set.

Read BYTE indexing

BYTE index zero selects the most significant byte of the word. This indexing direction differs from lower-order byte offsets used in Solidity packed storage.

Handle large shifts and indices

Boundary cases have specified outcomes. Do not rely on host-language shift operators that silently reduce the shift amount to a smaller machine width.

Trace the idea

0x1234 SHR 8 = 0x12
0x12 SHL 8 = 0x1200
BYTE(31, word 0x1234) = 0x34

Stack examples list the bottom first and the top last. These small traces omit gas and surrounding dispatch unless stated.

QUICK CHECK · NO PRESSURE

Which byte does BYTE index zero select?

Your progress stays in this browser.
Keep exploring the original referenceethereum.org ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.