Part 7: Bitwise Logic, Masking, and Shift/Rotate Instructions in x86-64
Lesson Overview & Bit Manipulation
Bitwise operations form the foundational bedrock of systems engineering, cryptography, network packet parsing, hardware device driver register control, and graphics algorithms. In assembly language, bitwise manipulation executes directly in the ALU hardware without compiler abstraction overhead. In Part 7, we explore boolean operations (AND, OR, XOR, NOT), non-destructive testing with TEST, logical vs. arithmetic bit shifts (SHL, SHR, SAR), circular rotations (ROL, ROR), and hardware population count instructions.
1. Boolean Logic Instructions
The primary bitwise logic instructions perform bit-by-bit boolean operations on register or memory operands:
| Mnemonic | Operation | Common Systems Programming Idiom |
|---|---|---|
| AND dest, src | Bitwise AND. Clears bits where src is 0. | and rax, 0xFF (mask low byte) and rax, ~15 (align to 16 bytes) |
| OR dest, src | Bitwise OR. Sets bits where src is 1. | or rax, (1 << 3) (sets bit 3 active) |
| XOR dest, src | Bitwise Exclusive OR. Inverts bits. | xor eax, eax (fast register zeroing!) |
| NOT dest | One's complement inversion (flips all bits). | not rbx (inverts all 64 bits, flags unaffected) |
| TEST dest, src | Non-destructive AND (updates ZF and SF). | test rax, rax (checks if pointer is NULL) |
Why do modern compilers write xor eax, eax instead of mov rax, 0?
The instruction mov rax, 0 requires a 7-byte encoding (including a 32-bit immediate zero) and introduces a data dependency in the CPU pipeline. In contrast, xor eax, eax requires only 2 bytes of machine code (31 C0). Modern Intel and AMD processors have dedicated hardware "zeroing idioms" in the instruction decoder that clear the register with zero execution latency and without consuming an ALU execution port!
2. Bit Shifts: Logical vs. Arithmetic
Shift instructions displace the bits within a register to the left or right by an immediate count or the value stored in the CL register:
SHL (Shift Left)
Shifts bits left, filling emptied positions with zeroes. Equivalent to multiplying by $2^n$:
shl rax, 3 ; RAX = RAX * 8
SHR (Logical Right)
Shifts bits right for unsigned integers, inserting zeroes into the most significant bits:
shr rax, 2 ; RAX = RAX / 4 (unsigned)
SAR (Arithmetic Right)
Preserves the sign bit (MSB) for signed two's-complement integers during right shift:
sar rax, 1 ; Signed division by 2
3. Advanced Hardware Bit Operations (POPCNT & BSF)
Modern processors include specialized hardware instructions for algorithmic acceleration:
- POPCNT (Population Count): Counts the number of active 1-bits in a register in 1 cycle:
popcnt rax, rbx(critical for chess bitboards and bloom filters). - BSF / BSR (Bit Scan Forward / Reverse): Scans for the index of the first or last active bit set:
bsf rax, rdi.
Summary & Next Step
Bitwise operations allow fine-grained data manipulation and flag evaluation. In Part 8, we combine arithmetic and bitwise comparisons with conditional jump instructions (JE, JNE, JG, JL) and build high-performance loops with branch prediction awareness.
Related reading
Part 11: Linux 64-Bit System Calls, NASM Macros & Dynamic Memory Allocation
Learn low-level systems programming: Linux 64-bit SYSCALL table, multi-argument NASM %macro directives, file I/O operations, and sys_mmap memory allocation.
By Md. Harun-Or-Roshid
Part 10: Stack Frames, Procedures & The System V AMD64 ABI in Assembly
Master assembly subroutines: CALL, RET, stack frames, RBP base pointers, 16-byte stack alignment, and the official System V AMD64 ABI calling convention.
By Md. Harun-Or-Roshid
Part 9: Assembly Arrays, String Primitives & Hardware Streaming (REP MOVSB)
Master memory buffers and strings in x86-64 assembly: arrays, strides, and hardware block primitives including MOVSB, LODSB, STOSB, SCASB, and REP.
By Md. Harun-Or-Roshid
Part 8: Conditional Branching, Jump Instructions & Fast Loops in Assembly
Learn how conditional jumps work in x86-64 assembly using CMP, TEST, JE, JNE, JG, JL, JA, JB, and write high-performance loops with branch prediction.
By Md. Harun-Or-Roshid