Lesson 7 of 15

Bitwise Operations

Bitwise Operations

Bitwise instructions operate on individual bits:

InstructionOperationExample
and dst, srcBitwise ANDand rax, 0xFF
or dst, srcBitwise ORor rax, rbx
xor dst, srcBitwise XORxor rax, rax
not dstBitwise NOTnot rax
shl dst, countShift leftshl rax, 2
shr dst, countShift right (unsigned)shr rax, 1
sar dst, countShift 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:

  1. 5 AND 3 = 1 (binary: 101 & 011 = 001)
  2. 5 OR 2 = 7 (binary: 101 | 010 = 111)
  3. 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.