Bit tricks compilers use
Optimising compilers avoid slow instructions, especially division. The result is code that looks nothing like the source until you know the patterns. All the listings here are real GCC 13 output at -O2.
Masks and shifts
unsigned get_bits(unsigned x) { return (x >> 4) & 0xF; }mov eax, edi
shr eax, 0x4
and eax, 0xfShift right, then AND with a mask: "extract bits 4 to 7". Whenever you see shr followed by and with a constant like 0xf, 0xff or 0x3ff, write down which bit field is being read. Protocol parsers and flag checks are full of these.
Power of two test
int is_power_of_two(unsigned x) { return x && !(x & (x - 1)); }xor eax, eax
test edi, edi
je done ; x == 0 -> return 0
lea eax, [rdi-0x1]
test eax, edi ; (x - 1) & x
sete al ; 1 if the result was zero
movzx eax, alx & (x - 1) clears the lowest set bit, so it's zero only for powers of two. sete turns a flag into a 0/1 value: that's how C comparisons become bools. And xor eax, eax is just the shortest way to write eax = 0.
Multiply with lea
unsigned mul10(unsigned x) { return x * 10; }lea eax, [rdi+rdi*4] ; x * 5
add eax, eax ; * 2lea computes an address without touching memory, and the addressing mode can do base + index*scale with scale 1, 2, 4 or 8. Compilers use it as a cheap three-operand multiply-and-add. Treat lea with no memory access as plain arithmetic.
Division by a power of two
unsigned div8(unsigned x) { return x / 8; }
int sdiv8(int x) { return x / 8; }; unsigned
mov eax, edi
shr eax, 0x3
; signed
test edi, edi
lea eax, [rdi+0x7]
cmovns eax, edi ; negative? use x + 7
sar eax, 0x3Unsigned is a plain shift. Signed needs care: C rounds toward zero, but an arithmetic shift (sar) rounds toward minus infinity. Adding 7 to negative values first fixes that. So test + lea +7 + cmovns + sar 3 is simply "signed divide by 8".
Division by a constant: magic numbers
unsigned div10(unsigned x) { return x / 10; }mov eax, edi
mov edx, 0xcccccccd
imul rax, rdx
shr rax, 0x23 ; >> 35No div at all. 0xCCCCCCCD is about 2^35 / 10, so multiplying by it and shifting right by 35 gives x / 10, exactly, for every 32-bit x. Signed division by 3 looks like this:
movsxd rdx, eax
imul rdx, rdx, 0x55555556
shr rdx, 0x20 ; high 32 bits of the product
sar eax, 0x1f ; -1 if x was negative, else 0
sub edx, eax ; round toward zeroTo decode a magic number: divide 2^(32 + shift) by it and round. 2^35 / 0xCCCCCCCD ≈ 10, and 2^32 / 0x55555556 ≈ 3. Decompilers like Ghidra and IDA's Hex-Rays do this for you and show x / 10, but in a raw listing you'll meet these constants often: 0xCCCCCCCD (/10 or /5), 0xAAAAAAAB (/3 unsigned), 0x55555556 (/3 signed), 0x92492493 (/7).
Takeaway
When a block of arithmetic looks absurd, assume the compiler was clever, not the author. Shift-and-mask, lea arithmetic, sete/cmov, and magic multiplies cover a large share of what you'll see.