All topics
132

Thirty-two on/off switches in one number

Tier 1Locked

Where it runs

1. The problem

The rest of the course needs two low-level tools: working with the single bits of a number, and a little number theory. A bit is one binary digit, 0 or 1.

A user has permissions: read, write, delete, admin and a dozen more. The obvious way to store them is one true/false value per permission, in a small object or an array. It works. But now every user carries a whole set of true/false values, and the check "can they write?" has to reach into that set every time.

You can store them more compactly. A whole number sits in memory as a row of 32 bits, and each bit is already a yes/no switch. Pack all the permissions into one number. Then the database holds one field, you pass one value around, and any switch changes with one operation.

For that you need to read and change one bit without touching the others.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
Finding the number without a pair with XOR
findUnique([5, 3, 5, 6, 3])
nums
5
3
5
6
3
bits (column = what the bit is worth)
8421
acc0000
acc = 0

Every number appears twice except one. acc starts at 0, and a XOR b flips the bits of a wherever b has a 1.

Speed race
Counting the set bits of the same 32-bit number: test all 32, or clear one set bit per step

Bars: the 32 bits of the number, highest on the left, a tall bar for 1. Checking every bit only reads them; Kernighan's trick clears each 1 it counts, so its bars end at zero.

One operation is one step of the loop: one bit tested or one bit cleared. Every frame, each lane does the same number of operations, so the slow one falls behind.

Check every bit O(n)
0 operations
Kernighan's trick O(k)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 1,000
Check every bit1K · under 1 ms
Kernighan's trick250 · under 1 ms
n = 100,000
Check every bit100K · 1 ms
Kernighan's trick25K · under 1 ms
n = 10,000,000
Check every bit10M · 100 ms
Kernighan's trick2.5M · 25 ms

Recognise it

Bit manipulation

Say these words and it is this pattern

  • find the single or unique number using XOR
  • set, clear, toggle, or test a bit
  • pack many yes/no flags into one integer
  • count set bits or check a power of two

Looks similar, isn't

One operation is O(1) on a 32-bit value. Looping the bits is O(32), which counts as constant.

On LeetCode

Problems that read, set or count single bits of a number, such as counting 1 bits, comparing two numbers bit by bit or reversing the bits. A mask 1 << i with AND, OR and NOT handles one bit at a time.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy