Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 03 · DSA · Worked, and deeper · about 20 min to read

Play it as a journeyTutor comes with a plan

Bit Manipulation (thinking in bits)

Thinking in Bits
Thinking in Bits · Open board

The moves you can make directly on a number’s row of switches: keep what two rows agree on, turn one on, flip every switch, tell which disagree, slide the row along. This lesson is what those moves build: flags packed into one integer, the one item with no partner, every subset of a group, a faster way to count.

Before you start. One fact from Numbers for Real (signed ints, overflow, floating point): a computer stores every whole number as a row of ones and zeros, read like ordinary counting but in twos instead of tens, so 1100 is one eight and one four, twelve. Nothing past that: no calculus, just counting.

Lesson 27 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 1999: a two-word comment, and no name on it for seven years
  • A file’s three switches, and what you’re allowed to do to them
  • The trick XOR can do that the others can’t
  • The slide, and the line makes sense now
  • The operators, precisely
  • XOR, the whole algebra
  • Masks & flags, at scale
  • The essential tricks: n & (n-1) and n & -n
  • The pitfall that passes the sample, and the fastest popcount
  • Subset enumeration: bitmasks as sets
  • Where bits power real systems

Terms it explains: AND, OR, NOT, XOR, non-negative, lowest set bit, counts set bits, power of two, `n & -n`, Fenwick tree, `n & 1`, `x & (m-1)`, all `2ⁿ` subsets, bitmask DP.

Review DSA