Lesson 7 of 15
Bitwise Operations
Bitwise Operations
Bitwise instructions operate on individual bits:
| Instruction | Operation | Example |
|---|---|---|
and dst, src | Bitwise AND | and rax, 0xFF |
or dst, src | Bitwise OR | or rax, rbx |
xor dst, src | Bitwise XOR | xor rax, rax |
not dst | Bitwise NOT | not rax |
shl dst, count | Shift left | shl rax, 2 |
shr dst, count | Shift right (unsigned) | shr rax, 1 |
sar dst, count | Shift right (signed) | sar rax, 1 |
Common Patterns
Zero a register (fastest way):
xor rax, rax ; rax = 0 (faster than mov rax, 0)
Multiply by powers of 2:
shl rax, 3 ; rax = rax * 8
Divide by powers of 2:
shr rax, 2 ; rax = rax / 4 (unsigned)
Check if a bit is set (using AND):
and rax, 1 ; rax = lowest bit of rax (0 or 1)
Your Task
Compute and print:
- 5 AND 3 = 1 (binary: 101 & 011 = 001)
- 5 OR 2 = 7 (binary: 101 | 010 = 111)
- 3 shifted left by 1 = 6 (binary: 011 << 1 = 110)
Print: "1\n7\n6\n"
x86_64 runtime loading...
Loading...
Click "Run" to execute your code.