InteractLab

InteractLabBitwise & Number Systems

Bits & Bytes — eight little light switches

A byte is just eight light switches in a row. Each switch is worth double the one to its right — 1, 2, 4, 8, 16, 32, 64, 128. Flip the right switches and you can spell any number from 0 to 255. Then watch AND, OR, XOR, and shifts move those switches around, one column at a time.

Module 1A byte is 8 light switches
42decimal
128
64
32
16
8
4
2
1
7
6
5
4
3
2
1
0

The lit switches add up: 32 + 8 + 2 = 42. Tap any switch to flip that bit.

Module 2Decimal → binary, by hand
step 142 ÷ 2 = 21 remainder0
step 221 ÷ 2 = 10 remainder1
step 310 ÷ 2 = 5 remainder0
step 45 ÷ 2 = 2 remainder1
step 52 ÷ 2 = 1 remainder0
step 61 ÷ 2 = 0 remainder1

↑ read the remainders bottom-to-top

result — 42 in binary

7
6
5
4
3
2
1
0

verify: 32 + 8 + 2 = 42

Module 3The bitwise playground
A12
B10
resultkeep only the 1s that appear in BOTH rows above
7
6
5
4
3
2
1
0
12 & 10 =8binary 00001000

keeps a bit only when BOTH A and B have 1 in that position

Module 4Where you actually meet bitwise code

Even / odd check

Instead of n % 2 == 0, low-level code writes (n & 1) == 0. The last bit of any even number is always 0.

Power-of-two check

n & (n - 1) == 0 — a power of 2 has exactly one 1-bit. Subtracting 1 flips every bit below it, so AND gives 0.

File permission flags

chmod 755 is bitwise flags. Read = 4, Write = 2, Execute = 1. OR them together: 4 | 2 | 1 = 7, full access.

RGB colour masking

Pull red out of 0xFF5733 with (color >> 16) & 0xFF = 255. Every browser and image editor does this.

Multiply / divide by 2, fast

n << 1 is n × 2 and n >> 1 is n ÷ 2. Compilers lean on this internally for speed.

Interview patterns

Single number, counting bits, missing number, subset generation — all lean on XOR or AND. Once you see the pattern they get straightforward.