Let's start simpler: how many integers can we express with 1 bit? Just 2:
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:
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:
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.
Interview coming up?
Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can break down tricky coding interview questions.
No prior computer science training necessary—we'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe whenever.
You're in! Head over to your email inbox right now to read day one!