The power of 'powers of 2'

It's a common technique in the world of computers to keep things in powers of 2. For example, there's 1024 kilobytes in a megabyte, that's 2 ^ 10 (the ^ symbol represents to the power of). Your computer screen has over 16 million colours, or to be exact 2^8 x 3 (if you're wondering why we multiply by 3, it's for the three channels: red, green and blue).

Why do we like it? Well, computers are great for calculations, except for when it comes to storing fractions of numbers. How do you store 1/3rd? You could start with 0.333333333, but if you want it to be completely accurate, you better start buying memory... and more memory... and more. In fact, no amount of memory in existence could store it accurately. Even if you gathered together all the memory in the world and stored 1/3 to the highest number of decimal digits, then multiplied it by 3, you'd never get back to 1.

But wait, your computer can do it, you say? That's because your computer rounds up 0.999999999 to become 1, effectively fooling you into believing it's more accurate than it really is. So although this technique may help you, it doesn't help people who need pin-point accuracy.

For example, what if you needed to store Pi to its highest calculated accuracy of 2.7 trillion digits? You'd need 2,500 gigabytes of memory to store that. That's around 5 modern day hard-drives. That's a lot of memory just to do accurate trigonometry.

So what does decimal digits have to do with powers of 2? Well, If you're working on a computer with graphics, colors, using interpolation or modelling objects in motion, you'll want to avoid storing decimal values where possible. Inaccuracy in decimals numbers accumulates the more you depend on them. As an analogy, lets suggest you have a metre long ruler that is inaccurate by 1 millimetre. If you used it to measure a meter long fence, you'd only be inaccurate by 1 millimetre. But what if you used it to measure a 50 metre long swimming pool? You'd be out by 50mm. If you used it to determine a safe landing place for a spacecraft on mars, your craft would miss the planet entirely!

Now let's say you take a picture with your digital camera and shrink it down to 128x128 pixels in size and save it. The next day you want to reduce it even further by a half (1/2). Well it's a good thing we chose 128x128 because half of 128 is 64, so we've successfully avoided fractions. Both 128 and 64 are powers of 2. So by sticking to powers of 2, we're more likely to avoided fractions all together. What about if we shrink your picture to a quarter (1/4)? That's 32 pixels, still no fractions. 1/8th the size? 16 pixels, no big deal. What happens if we don't use powers of 2? Well let's suggest that we like using numbers in increments of 10, such as 10, 20, 30 etc. Now, 128 doesn't work with increments of 10, so let's make it 130. Think about what happens when we shrink the image. A half is 65 pixels. A quarter is 32.5 pixels. Wait? 0.5 pixels? You can't have 0.5 pixels on a screen. So you're going to have to round it. If you round down to 32 pixels you're trimming off an edge, and if you round up to 33 pixels and you're duplicating an edge. Neither solution is optimal and both distort the image from its intending proportions. All this could have been avoided if we just used a power of 2.

You can see why powers of 2 are important when doing division on a computer. It's not just division that it's useful for, there are many other applications of powers of 2. Let's talk about Binary. It's no surprise that the base unit of every personal computer available today uses an elegant system involving powers of 2.

Here's the fundamental law: Any whole number can be written by employing powers of 2. And all we need is many on and off switches. It may come as a surprise that any number can be written with powers of 2, and it can be written faster and with more compression than any counting method.

Take the number 23. The first method, which I call the Counting Method, involves turning 23 switches to the 'on' position. 23 switches is quite a lot just for storing a number, and it can spiral out of control with extremely large numbers. Can we do it in less?

The second method I call the Representation Method. This involves assigning the switches to represent values. Each switch represents a number and the number it represents can be greater than 1. Here's an arrangement of numbers that might look familiar:

switch number
A 1
B 2
C 4
D 8
E 16

Do you see the pattern? Each number is a power of 2. A is 2^0, B is 2^1, C is 2^2, and so on.

What happens if we turn on switch A,B, C and E and add the values of the turned on switches?

1 + 2 + 4 + 16 = 23. We wrote the number 23 with only 5 switches, 4 turned on and 1 turned off. That's far less than the 23 switches we used in the Counting Method. Amazing! We didn't get lucky, as long as you've got enough switches arranged in powers of 2, you can represent any number in a short-hand form. The larger the number, the more switches you need, but it's nowhere near as inefficient as the Counting Method. And remember, all a computer is comprised of is a series of on-and-off switches. How we use these switches dictates how effective our computer is. Obviously the Representation Method is the superior method.

But wait, surely there's an even better way to store data though. Switches still can't deal with fractions very well, as we discussed earlier. Couldn't we use cups of water? If the cup is half full, the number is 0.5? Finally we'd the get accuracy we'd need!

This is one of the problems facing scientists today. Finding a method of accurately storing fractions would revolutionise the computer industry, making today's computers look like out-dated calculators by comparison. The problem is finding a stable material to store fractions of units. Remember, liquid expands and can even evaporate. No material keeps it's current state without some fraction of change, which over time makes an exact measurement lose its accuracy. Some scientists believe the answer lies in quantum physics, but even then, maintaining stability is difficult and the techniques required are expensive.

So clearly we can see the power of 'powers of 2' and why we abide to them when we can. Overcoming fractions on computers is a small inconvenience, but a necessary one... at least until that quantum computer becomes a household item.