Skip to the workspace
Pebellslearn by building
Bits & Machine Numbers  ›  Lesson

AND, OR, XOR & Shifts

Bitwise Operators 25 minEvery integer is a row of bits, and five operators work on the whole row at once

dsa / AND, OR, XOR & Shifts

Explore each scene at your own pace.

01 · Meet Quarry

Meet Quarry

AND, OR, XOR & Shifts

Definition
Bitwise operators act on the binary digits of integers, with & keeping bits set in both, | keeping bits set in either, ^ keeping bits that differ, ~ flipping every bit, and << and >> shifting the bits left or right.
In plain words
An integer is a row of on-off switches, and these operators work on the whole row at once. They can compare two rows switch by switch, flip every switch, or slide the whole row along, which multiplies or divides by two.

In short: an integer is a row of bits, and four operators act on the whole row at once: & keeps a bit only where both sides have it, | keeps a bit where either side has it, ^ keeps a bit where the sides differ, and ~ flips every bit. << and >> slide the whole row left or right, which multiplies or floor-divides by a power of two. Python ints have no fixed width, so any trick that depends on a register's bits running out (wraparound, overflow) needs an explicit mask.

Four bit columns, x=12 and y=10 stacked above the AND, OR and XOR rows, with the AND row highlighted green because it keeps only the column where both are 1

Meet Quarry

For six sections you built Tunebox, a music app. From here the course builds something harder: Quarry, a search-and-storage engine. Think of the part of a database that nobody sees. It takes records in, stores them compactly, finds them again quickly and keeps them safe as they change. Over the coming sections Quarry gets a record encoder, a hash kernel, range analytics, a query planner, a text index and a diff engine. Each one is a real subsystem of a real engine, and each needs an algorithm you haven't met yet.

It starts at the bottom, with the record header. Every record Quarry stores carries four small facts: its id, the shard it lives on, its type, and the epoch (a version counter) it was written in. Held as a Python dict, that costs about two hundred bytes per record. Packed into a single 32-bit word, it costs four:

python

Across a hundred million records that is the difference between 20 GB and 400 MB. The line that builds word uses two operators this lesson is about: << and |. By the end of the section you'll have written the whole encoder yourself. You'll pack and unpack the fields, check them, and index them with bitsets.

Scene 1 of 10
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — changed_flags(): Reports which status bits a write switched on and which it switched off.
Scenario A write lands on a Quarry record and its status word changes. Before it's logged, the change detector reports exactly which flags went on and which went off, so the flusher knows the record is dirty and the cache knows whether it was unpinned.
changed_flags.py
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.