Powers of Two in Computing: Why 4, 8 and 256 Keep Showing Up
Why do computers use powers of two? Because a computer stores everything as bits, and each extra bit doubles the number of patterns it can hold. That is why powers of two in computing are everywhere: 8 bits in a byte, 256 values in that byte, 1024 bytes in a kibibyte. This guide shows where the doubling comes from, then follows one pixel through the code of a small browser tank game, where 4 and 8 do real work.
Powers of two in computing: the short answer
A bit has one of two possible values, written 0 and 1. If you need a refresher, the post on what a bit is in a computer covers it in plain words.
Put bits side by side and count the patterns. One bit gives 2. Two bits give 4. Three bits give 8. Each new bit doubles the total, so n bits give 2 multiplied by itself n times, written 2 to the n.
So powers of two are not a style choice. They come straight out of counting with two-way switches. Once you store numbers in bits, the natural sizes and limits line up with 2, 4, 8, 16, 32, 64, 128, 256 and on up.
Why each extra bit doubles the count
Think of every pattern you could make with three bits. There are 8 of them, from 000 to 111. Now add a fourth bit on the left.
Every old pattern is still there, once with the new bit set to 0 and once with it set to 1. That is 8 patterns twice, so 16. Add a fifth bit and the same thing happens again: 32.
This is also why binary place values are powers of two. Reading from the right, the places are worth 1, 2, 4, 8, 16, 32, 64 and 128. Take 1101: that is 8 + 4 + 0 + 1, which is 13.
Here is a small real case. Tank City Reboot stores each square of its field as one of 7 kinds: Empty, Brick, Steel, Water, Fog, Ice and Base. Two bits only give 4 patterns, which is not enough. Three bits give 8, which fits 7 kinds with one pattern to spare.

Each step down the ladder adds one bit and doubles the count. The right side shows where each number turns up in Tank City Reboot.
Bytes, 256 and 1024
A byte is 8 bits. Eight bits give 2 to the 8th power, which is 256 patterns. Counting from 0, a byte holds the values 0 to 255. That is why 255 turns up so often as a top value.
Colors are a good example. A common way to store a pixel is one byte each for red, green, blue and alpha (how see-through the pixel is). That format is called RGBA32: 4 bytes, so 32 bits, per pixel. With 8 bits per channel, each channel has 256 levels.
Every texture in Tank City Reboot is made in this format by one small function in the code. So each pixel in the game is 4 bytes, and each channel of it is a number from 0 to 255.
Then there is 1024, which is 2 to the 10th power. It sits close to 1000, which is how 1024 bytes came to be called a kilobyte. Today the standard name for 1024 bytes is a kibibyte (KiB), and a kilobyte (KB) can mean 1000. The FAQ below has the short version.
Powers of two in Tank City Reboot's code
Tank City Reboot is a free browser game where your antivirus tank guards the CPU core at the bottom of a circuit board. Much of its art is drawn in code, so you can see exactly which sizes it picks.
Look at how the field is built:
- The board is 208 by 208 pixels.
- It is stored as 52 by 52 small squares, each 4 by 4 pixels.
- Each square holds one of the 7 kinds above, stored in one byte.
Many textures are powers of two. Tanks, the core and power-ups are 16 by 16. The soft glow and the ring are 32 by 32. A shot is 4 by 4. The scan line overlay is a 64 by 64 texture that repeats across the screen.
Not every size is a power of two, and that is fine. The shield is 22 by 22, the small tank icon is 7 by 7, and the board itself is 208 by 208. Powers of two matter most where the code does fast math on positions, which is the 4 pixel square.
A worked example: one pixel, one square
When the game paints the board, it walks over every pixel and asks which square that pixel belongs to. The code writes this as px >> 2. The >> operator shifts the bits right. Shifting right by n is the same as dividing by 2 to the n and rounding down, so shifting by 2 divides by 4.
Take pixel x = 150. In binary that is 10010110.
- Which square?
150 >> 2drops the last two bits and leaves100101, which is 37. So the pixel is in square 37. - Where inside it?
150 & 3keeps only the last two bits,10, which is 2. So it is the third pixel of that square, counting from 0. - Check: 37 times 4, plus 2, is 150.
The square number and the position inside it are just two slices of the same binary number. That only works because 4 is a power of two. If squares were 5 pixels wide, the last few bits would not line up with the square edges, and the code would need a real division instead of a shift and a mask.
The brick pattern uses the same idea. A brick pixel is mortar when py & 3 is 0, which gives a line on every fourth row, or when (px + off) & 7 is 0, which gives a line every eighth column. Here & 7 is the remainder after dividing by 8, and again that only works because 8 is a power of two. For each operator in detail, see what bitwise operators are in programming.
When a power of two runs out
A fixed number of bits has a fixed ceiling. A byte stops at 255. Add 1 to 255 in a single byte and there is no ninth bit to carry into, so the value wraps back to 0.
That ceiling is just 2 to the n minus 1, where n is the number of bits. Knowing the power of two tells you the limit at once: 16 bits stop at 65,535, and 32 bits at a little over 4 billion. What happens when a program goes past that limit is a story of its own, told in the post on integer overflow in programming.
Frequently asked questions
Why is a kilobyte 1024 and not 1000?
1024 is 2 to the 10th power, the power of two closest to 1000, so it was used as a binary kilobyte. The standard name for 1024 bytes is now a kibibyte (KiB), though many people still say KB for it.
Why is 255 the top value of a color channel?
Each channel is stored in one byte of 8 bits. Eight bits give 256 values, and counting from 0 makes the top value 255.
Does every texture have to be a power of two?
Not in Tank City Reboot. Its shield is 22 by 22 and its board is 208 by 208, while tanks are 16 by 16 and the scan line tile is 64 by 64.
Why is shifting right by 2 the same as dividing by 4?
Each place in binary is worth twice the place to its right. Dropping the last two places divides by 2 twice, which is 4, and the dropped bits are the remainder.
Get started
Open the first game and look closely at a brick wall. Its mortar lines fall on every fourth row and every eighth column, drawn by the & 3 and & 7 you just followed. The game teaches the idea behind each name it uses; it is not a course.
Play Tank City Reboot: it is free, runs in your browser on a computer or a phone, and playing never needs an account.
Comments
No comments yet.
Sign in or make an account to comment.