SYSTEMS / Learn
Saved on this device>
← Back to goals

I want to…

Clear all bits

Trace this example, understand why the operation works, then try it from memory.

1 · Choose a goal → 2 · Learn the method → 3 · Practise → 4 · Spaced review

Learn the method

x ^ x = 08-bit word · arithmetic wraps modulo 256
BIT-BY-BIT WALKTHROUGH
OperatorBits 7 → 0Decimal
x1001 10012153
x (XOR)1001 10012153
Result0000 000020

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.

IN CODEC17
#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, rsi

x86-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

coffee-bytes / Systems LearningCS:APP + Hacker’s Delight