Skip to content

Binary and Bitwise Ops

Lesson 1 of 5 · 4 min

Binary and Bitwise Ops

Step 1 of 10

To the CPU, 12 is eight switches — the 8 lane and the 4 lane are the only ones on.

The Idea

Bitwise operators work lane by lane, with no carries between lanes. & keeps a 1 only where both sides have one, | where either does, ^ where they differ.

~ flips every lane. Shifts slide the whole row: << doubles, >> halves.

Real-World Example

File permissions are nine bits wide: read, write and execute for owner, group and others. chmod 644 is literally a number, and the kernel answers "may this process write?" with a single AND against a mask — no lookup, no branch.

The Code

Your turn

What does this print?

x = 5          # 0b101
y = 3          # 0b011
print(x & y, x | y, x ^ y)

Mini quiz

1 / 3

What is `13 & 6`?