Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. bitwise
5 min read

Bitwise Ops

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, not

Four instructions apply a boolean operation bit by bit. The first three take the usual two operands (destination first); not takes one:

nasm
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 bit

Each has a characteristic job:

OpBit ruleUsed to
and1 only where both are 1clear bits — a 0 in the mask forces that bit off
or1 where either is 1set bits — a 1 in the mask forces that bit on
xor1 where the bits differtoggle bits, or test two values for equality
notflipinvert every bit (one's complement)

Shifts and Rotates

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:

gdb
gef➤ break _startBreakpoint 1 at 0x401000gef➤ runBreakpoint 1, 0x0000000000401000 in _start ()gef➤ break *0x401008Breakpoint 2 at 0x401008gef➤ continueBreakpoint 2, 0x0000000000401008 in _start ()gef➤ p/x $rax$1 = 0x1e0gef➤ break *0x401012Breakpoint 3 at 0x401012gef➤ continueBreakpoint 3, 0x0000000000401012 in _start ()gef➤ p/x $rbx

shl 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.

Masking and Testing

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.

The xor reg, reg Zeroing Idiom

To set a register to zero, assembly almost never uses mov rax, 0. Instead:

nasm
xor rax, rax        ; rax = 0

A 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.

Common Bit Tricks

A few patterns recur so often they are worth memorizing:

  • Zero a register: xor rax, rax.
  • Test for zero / NULL: test rax, rax then a conditional jump.
  • Modulo a power of two: and rax, 7 computes rax % 8 (mask off all but the low 3 bits) — far cheaper than div.
  • Multiply / divide by a power of two: shl/shr (or sar for signed).
  • Check bit k: test rax, 1 << k sets ZF if that bit is clear.
$2 = 0x7ffffffffffffff8
gef➤ break *0x40101c
Breakpoint 4 at 0x40101c
gef➤ continue
Breakpoint 4, 0x000000000040101c in _start ()
gef➤ p/x $rcx
$3 = 0xfffffffffffffff8
gef➤ p/d $rcx
$4 = -8
gef➤ break *0x401028
Breakpoint 5 at 0x401028
gef➤ continue
Breakpoint 5, 0x0000000000401028 in _start ()
gef➤ p/x $rdx
$5 = 0x78