Begin Algo
Algorithms · 4 lessons

Bit ManipulationWorking directly with ones and zeroes

An integer in memory is just a run of bits. AND, OR, XOR and the shifts are each a single CPU instruction, and used well they collapse some problems to constant time — or let one integer stand in for a whole set.

Why learn Bit Manipulation

Where it shows up
Permissions and feature flags

Read, write and execute take one bit each, so one integer holds the lot. Unix's chmod 755 and a game's status flags work this way.

→ Lesson: Bitwise Basics
Finding the one that is alone

Every value appears twice except one. XOR them all together and the pairs cancel, leaving the answer — with no extra memory at all.

→ Lesson: XOR Tricks
Netmasks and hashing

Subnet masks, replacing a hash table's modulo with an AND, Bloom filters — all bit operations underneath.

→ Lesson: Counting Bits

Lessons

4 lessons