Why powers of twos?

Started by
4 comments, last by googlyeyes 15 years, 6 months ago
Why are powers of twos so special in programming? I looked on wiki and it said that in binary representations they're always 100...0 but why's that so special?
Here to learn :)
Advertisement
Each binary place represents a double.
So a 1 bit number is either 1 or 0
2 bit 0,1,2,3 (4 numbers) or 2^2
3 bit 0,1,2,3,4,5,6,7 (8 numbers) 2^3
4 bit number 0,...,15 (16 numbers) 2^4
A binary system is always going to have 2^(number bits) unique number reprensenations.
Powers of two are important because numbers in computers are represented in binary form. I'm going to assume you know a little bit about binary, but if you have any questions about binary numbers please ask.

Basically in a computer, everything is a "switch". Things are either on or off. Computers work on electricity, and each binary digit is a "bucket". Inside each bucket there can be an electrical charge. If there is, that "bucket" is "on", if there isn't an electrical charge in the bucket, the bucket is "off". By stringing a bunch of buckets together (or binary digits), you can create an entire binary number.

Here is a great link to an article that talks about binary numbers and how and why they are used in computers.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by MrPickle
Why are powers of twos so special in programming? I looked on wiki and it said that in binary representations they're always 100...0 but why's that so special?
It isn't just powers of 2, it is the number 2 itself. Computers represent numbers in binary - i.e. base 2. 2 is as important in binary as 10 is in our normal decimal system, and similarly, powers of 2 are just as important as powers of 10 in normal use.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

It's a base-2 number system. Which means that any value that is a power of two is going to have a single digit set to "1" followed by zero or more "0"s. If you had a base-43 number system, then it follows that any value that is a power of 43 would also be represented by a single "1" digit followed by zero or more "0"s.

Google "number systems" to learn more!
EDIT: OMG Ninja'd four times!!

Okay I'm terrible at explaining but here goes:

Long story:
Computers store data physically in a string of binary states which are represented by 0's and 1's.
So we can represent a block of computer data like so:
10011101. This is one byte which is a somewhat arbitrary length for a block of data.
Notice that the highest number you can fit in one byte is 11111111, or in decimal 255. This is 2^8-1 (2 to the power of eight minus one). A power of two minus one!
It's a power of two minus one because there are two possibilities for each bit and we start from zero, not one.

A 2-bit number has four possibilities:
00 (0)
01 (1)
10 (2)
11 (3)
Notice that the number of possibilities is 2^2 and the highest number is 2^2-1.

So going up by number of bits the number of possible states are:
2^1 (2)
2^2 (4)
2^3 (8)
2^4 (16)
2^5 (32)
2^6 (64)
2^7 (128)
2^8 (256)
All powers of two!

You may also wonder why computers store binary values (0-1) instead of decimal (0-9) or something else (like 0-2). They actually do this because it's the cheapest and easiest way to store information on a physical level. It's easier to tell if a voltage is above or below a certain threshold than to check for any number of possible states.

Short story:
Computers store information in base two because it's easiest. Each "digit" has two possible states so when we put more n next to each other there's 2^n possibilities (because of math) so 2*2*2*2... n times.

This topic is closed to new replies.

Advertisement