Where arithmetic treats a register as a single number, the bitwise instructions treat it as 64 independent bits and operate on each one in parallel. They are the tools for picking apart a value — isolating a field, flipping a flag, testing whether a particular bit is set — and they show up constantly in low-level code, from permission masks to the compiler's own shortcuts.
and, or, xor, notFour instructions apply a boolean operation bit by bit. The first three take the usual two operands (destination first); not takes one:
1and rax, rbx ; each bit: 1 only if set in both
2or rax, rbx ; each bit: 1 if set in either
3xor rax, rbx ; each bit: 1 only if the two differ
4not rax ; flip every bitEach has a characteristic job:
| Op | Bit rule | Used to |
|---|---|---|
and | 1 only where both are 1 | clear bits — a 0 in the mask forces that bit off |
or | 1 where either is 1 | set bits — a 1 in the mask forces that bit on |
xor | 1 where the bits differ | toggle bits, or test two values for equality |
not | flip | invert every bit (one's complement) |
A shift slides every bit left or right by a given count, which is a fast multiply or divide by a power of two. There are three, and the difference between the two right shifts is the thing to get right:
shl — shift left; the vacated low bits fill with 0. Shifting left by n multiplies by 2ⁿ.shr — logical shift right; the vacated high bits fill with 0. This divides an unsigned value by 2ⁿ.sar — arithmetic shift right; the vacated high bits copy the sign bit. This divides a signed value by 2ⁿ, keeping negatives negative.Watch all three, plus a mask, run against real values:
break _startBreakpoint 1 at 0x401000runBreakpoint 1, 0x0000000000401000 in _start ()break *0x401008Breakpoint 2 at 0x401008continueBreakpoint 2, 0x0000000000401008 in _start ()p/x $rax$1 = 0x1e0break *0x401012Breakpoint 3 at 0x401012continueBreakpoint 3, 0x0000000000401012 in _start ()p/x $rbxshl turned 0xf0 into 0x1e0 — doubled. The two right shifts started from the same value, -16 (0xfffffffffffffff0), and diverged: shr zero-filled the top and produced the huge positive 0x7ffffffffffffff8, while sar copied the sign bit down and produced 0xfffffffffffffff8 — which is -8, exactly -16 / 2. Use shr for unsigned values and sar for signed ones, or a divide-by-two silently turns a negative number into a giant positive one.
Two cousins, rol and ror, rotate instead of shift: bits that fall off one end wrap around to the other rather than being lost. They matter mostly in cryptography and hashing.
Pairing and with a mask — a constant whose 1 bits mark what to keep — isolates part of a value. That last step of the session did exactly this: and rdx, 0xFF kept only the low byte of 0x12345678, leaving 0x78.
Often you want to check some bits without keeping the result. That is test: it computes and purely to set the flags, then throws the result away. The idiom you will see everywhere is test rax, rax — and-ing a value with itself changes nothing but sets ZF when the value is zero, so it is the standard "is this zero or NULL?" check, cheaper than cmp rax, 0. The flags it sets feed straight into the conditional jumps of Control Flow.
xor reg, reg Zeroing IdiomTo set a register to zero, assembly almost never uses mov rax, 0. Instead:
xor rax, rax ; rax = 0A value xor'd with itself is always zero, and this encoding is shorter (xor eax, eax is two bytes versus five for mov) and lets the CPU break the register's dependency on its old contents, which helps it run ahead. Compilers emit it universally — so recognizing xor reg, reg as "set to zero," not a real exclusive-or, is essential to reading disassembly. One caveat: unlike mov, it sets the flags (ZF becomes 1), which just occasionally matters.
A few patterns recur so often they are worth memorizing:
xor rax, rax.test rax, rax then a conditional jump.and rax, 7 computes rax % 8 (mask off all but the low 3 bits) — far cheaper than div.shl/shr (or sar for signed).test rax, 1 << k sets ZF if that bit is clear.