# Why powers of twos?

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by MrPickleWhy 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.

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632910
• Total Posts
3009176
• ### Who's Online (See full list)

There are no registered users currently online

×