Learn the method
x ^ x = 08-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
x (XOR) | 1001 10012 | 153 |
Result | 0000 00002 | 0 |
Each bit is paired with an identical copy. XOR is 1 only for different inputs, so every column becomes 0. The identity x ^ x = 0 works at any word width; xor rsi, rsi uses it to clear a 64-bit register.
#include <stdint.h>
uint8_t x = 153;
uint8_t result = x ^ x;C17 · snippets use uint8_t from <stdint.h>. Casts keep the low 8 bits; shifts use unsigned operands. Run the statements inside a function.
REGISTER IDIOM
xor rsi, rsix86-64 · Intel syntax. XORing RSI with itself clears all 64 bits, regardless of its initial value. This also sets ZF and clears CF and OF; it is not flag-equivalent to mov rsi, 0. The diagram shows the same identity in an 8-bit word.
CS:APP · §2.1.8