Quiz Entry - updated: 2026.07.10
What does the test instruction do and when is it used?
test computes a bitwise AND of its operands and sets the flags, but throws the result away — it's and without the side effect of changing the destination.
test is the idiomatic way to ask yes/no questions about a value without disturbing it. Three classic uses:
1. Is a register zero? ANDing a value with itself is zero only if the value is zero:
test %rax, %rax
jz is_zero
2. Is a particular bit set? AND with a mask:
test $1, %al # bit 0 — is the number odd?
jnz is_odd
test $0x80, %al # bit 7 — the sign bit
jnz is_negative
3. Is a pointer NULL?
test %rdi, %rdi
jz handle_null
Why test %rax,%rax instead of cmp $0,%rax? It produces a shorter encoding (no immediate to store) and is the conventional zero-check, so compilers emit it constantly — recognizing it is essential when reading disassembly.
Go deeper:
Felix Cloutier — TEST — the bitwise-AND-that-sets-flags-and-discards-result semantics.