These operations combine corresponding bits across words.
LET’S PICTURE IT
A transparent stencil can retain, combine or highlight selected positions.
SEE IT FOR YOURSELF
Watch a stack machine execute a tiny programStep 1 / 4
The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.
The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.PUSH1 reads its next byte as an immediate value and places two on the stack. The next instruction begins at byte two.The next PUSH1 places three above two. Stack order matters for operations that are not commutative.ADD consumes the top two values and pushes five. The next instruction is STOP at byte five; no storage write is implied by ADD.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Use AND as a mask
AND retains a bit only where both inputs have that bit set. Masking with 0xff keeps the low eight bits and clears the higher bits.
Use OR to combine fields
OR sets a bit where either input has it set. When combining packed fields, ensure the intended regions do not overlap accidentally.
Use XOR for differences
XOR sets a bit where the two input bits differ. Applying the same XOR mask twice restores the original bit pattern.
Separate bit operations from booleans
These are full-word bitwise operations. A Solidity boolean expression and a word mask may use related instructions but have different type-level meaning.
Trace the idea
0x1234 AND 0x00ff = 0x0034
0x1200 OR 0x0034 = 0x1234
0x1234 XOR 0x00ff = 0x12cb
Stack examples list the bottom first and the top last. These small traces omit gas and surrounding dispatch unless stated.