Bits Number Of Possibilities

Let's start simpler: how many integers can we express with 1 bit? Just 2:

Two unconnected nodes, labeled 0 and 1.

How many can we express with 2 bits? Well for each possibility for the first bit, we can put a 0 after it or we can put a 1 after it:

Two unconnected nodes, labeled 0 and 1, each with two children. The 0 node has children labeled 00 and 01 while the 1 node has children labeled 10 and 11.

So by adding a second bit, we have twice as many possibilities as with just 1 bit. 2 * 2 = 4 possibilities in total.

Same idea with 3 bits—for each of the 4 possibilities with 2 bits, we can put a 0 after or we can put a 1 after. That gives us twice as many possibilities as with 2 bits, which is twice as many as with 1 bit, so 2 * 2 * 2 = 8:

Two unconnected nodes, labeled 0 and 1, each the root of a binary tree. The 0 node has children labeled 00 and 01. The 00 node has children labeled 000 and 001 while the 01 node has children labeled 010 and 011. The 1 node on the other tree has children labeled 10 and 11. The 10 node has children labeled 100 and 101 while the 11 node has children labeled 110 and 111.

Do you see the pattern? In general, with n bits, we can express 2^n different possible numbers!

So with 8 bits, that's 2^8=256 different numbers.

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .