Module 11 · Bits, Range Tools and Final Review · Lesson 37 of 40
Bit manipulation
Numbers as 0s and 1s, the operators & | ^ ~ << >>, and neat tricks like checking powers of two and the XOR single-number puzzle.
In this lesson · about 22 minutes
- Read small numbers in binary
- Use &, |, ^, << and >> to check, set, clear and flip bits
- Solve classic bit puzzles with XOR and n & (n − 1)
Inside the computer every number is stored in binary: only 0s and 1s, called bits. Each position is worth double the one to its right: 1, 2, 4, 8, 16…
13 in binary is 1101: 8 + 4 + 0 + 1. In Python, bin(13) shows '0b1101' and 0b1101 is 13. Bit positions are counted from the right, starting at 0.
The bit operators
| Operator | Name | Rule, bit by bit | Example |
|---|---|---|---|
& | AND | 1 only if both are 1 | 12 & 10 = 8 |
| | OR | 1 if either is 1 | 12 | 10 = 14 |
^ | XOR | 1 if they are different | 12 ^ 10 = 6 |
~ | NOT | flips every bit (in Python, ~x is -x - 1) | ~5 = -6 |
<< k | left shift | move bits left: multiply by 2ᵏ | 3 << 2 = 12 |
>> k | right shift | move bits right: divide by 2ᵏ (rounding down) | 13 >> 1 = 6 |
Try it · see it work
Bit playground
Click bits to flip them and watch every result change. Try making a equal to 8, 16 or 32.
Click the bits of a and b to flip them. Bit positions are numbered 7…0 from left to right.
& keeps a 1 only where both have 1. | keeps a 1 where either has 1. ^ (XOR) keeps a 1 where they differ. << 1 doubles, >> 1 halves (rounding down). Is a even? Yes: its last bit is 0.
Working with one bit (a "mask")
1 << i is a number with only bit i turned on. Use it as a mask:
| Job | Code |
|---|---|
| Is bit i on? | (n >> i) & 1 or n & (1 << i) != 0 |
| Turn bit i on | n | (1 << i) |
| Turn bit i off | n & ~(1 << i) |
| Flip bit i | n ^ (1 << i) |
| Is n odd? | n & 1 == 1 |
Famous tricks
- n & (n − 1) removes the lowest 1-bit. So a positive n is a power of two exactly when
n & (n - 1) == 0(it has a single 1-bit). - Counting 1-bits (Brian Kernighan): keep doing
n &= n - 1and count the steps. Python also hasbin(n).count("1"). - XOR magic:
x ^ x = 0andx ^ 0 = x, in any order. So if every number appears twice except one, XOR-ing them all leaves the single one. O(n) time, O(1) space. - Subsets as numbers: with n items, each number from 0 to 2ⁿ − 1 is a subset, where bit i says whether item i is in.
Try it: bits in Python
Exercise
The number without a partner
Every number in the list appears twice, except one. Write single(nums) that returns the lonely number using XOR (no dicts, no sorting).
Exercise
Count the 1-bits
Write ones(n) that counts the 1-bits of a non-negative number with Brian Kernighan's trick: while n is not 0, do n &= n - 1 and add 1 to a counter.
Check your understanding0 of 4 correct
Quiz score: 0 of 4
Exercises solved: 0 of 2
Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.