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

OperatorNameRule, bit by bitExample
&AND1 only if both are 112 & 10 = 8
|OR1 if either is 112 | 10 = 14
^XOR1 if they are different12 ^ 10 = 6
~NOTflips every bit (in Python, ~x is -x - 1)~5 = -6
<< kleft shiftmove bits left: multiply by 2ᵏ3 << 2 = 12
>> kright shiftmove 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.

a= 12
b= 10

a & b00001000= 8
a | b00001110= 14
a ^ b00000110= 6
a << 100011000= 24
a >> 100000110= 6

& 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:

JobCode
Is bit i on?(n >> i) & 1 or n & (1 << i) != 0
Turn bit i onn | (1 << i)
Turn bit i offn & ~(1 << i)
Flip bit in ^ (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 - 1 and count the steps. Python also has bin(n).count("1").
  • XOR magic: x ^ x = 0 and x ^ 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

Question 1

1.What is 6 in binary?

Question 2

2.What is 5 ^ 5?

Question 3

3.For a positive n, when is n & (n − 1) equal to 0?

Question 4

4.What does 3 << 2 give?

Quiz score: 0 of 4

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

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.