# Why is chunk size often a power of two?

There are many Minecraft clones out there and I am working on my own implementation. A principle of terrain rendering is tiling the whole world in fixed size chunks to reduce the effort of localized changes.

In Minecraft the chunk size is 16 x 16 x 256 as far as I now. And in clones I also always saw chunk sizes of a power of the number 2.

Is there any reason for that, maybe performance or memory related? I know that powers of 2 play a special role in binary computers but what has that to do with the chunk size?

• It's nice that you can keep dividing it by two and get even numbers back. (Not a complete answer, but something useful about using a number like 2^n) – ashes999 Jan 8 '13 at 22:37

This would depend on the game and the indexing structure used for the chunks. Though, at such a high level, it's not too likely it has much to do with memory or a specific performance enhancement. More than likely it's an arbitrary decision for sizing chunks in a predictable way. It allows for some counting and indexing tricks using bit shifting that wouldn't be possible with numbers that aren't a power of two.

For example, counting in powers of two is as easy as shifting a bit in binary:

Dec =  Bin
1   =  000001
2   =  000010
4   =  000100
8   =  001000
16  =  010000
32  =  100000


Where these shortcuts will be used will depend on the developer and what problem they're trying to solve.

If you're making a decision for what size to make the chunks, and it doesn't matter in any other aspects, you may as well use something that is familiar and has benefits you're used to using.

First, multiplying by powers of two is much cheaper than multiplying by an arbitrary number, since you can do it by bit shifting. Most of the time the compiler can do this for you, so whenever you write "* 16" in your code, the compiler actually does a shift by four, and you don't need to worry about it - you just need to give the compiler the opportunity by designing your data structures this way.

Second, since cache lines, memory busses and other information highways in your computer also tend to be designed to use powers of two, you may get a better performance overall this way.

Third, we old geeks just are used to playing around with powers of two, so it's a habit.

(Fourth, other old geeks who design your hardware and your compilers also like powers of two, so this is not going to change any time soon).

• +1 "Third, we old geeks just are used to playing around with powers of two, so it's a habit." This is probably the main reason. – Laurent Couvidou Jan 10 '13 at 18:24

The real answer is just this: On a binary computer, powers of two are nice round numbers.

When a normal person needs to pick an arbitrary number for some purpose, they typically choose nice round numbers in the number system they're comfortable with, base 10. So they'll pick 10, 100, 1000, etc. Because they're simple and easy and don't require much thought and the precise value wasn't really important to them, they were just aiming at a general scale of magnitude.

As programmers, when we need to pick an arbitrary number for some purpose, we typically choose nice round numbers in the number system computers use, base 2. So we'll pick 2, 4, 8, etc. Because they're simple and easy and don't require much thought and the precise value wasn't really important to us, we were just aiming at a general scale of magnitude.

It's really not any more complicated than that. They're just nice round numbers.

One reason not mentioned in other answers is that if needed, powers of two numbers can always be halved without rounding problems. This is probably not a reason in Minecraft clones, but is in some other cases such as in textures with mipmaps.

Another potential reason would be that it would allow you to encode info about each chunk in a 3D texture. If your target hardware supported 3D textures but didn't have fully robust and general non-power-of-two texture support (which I admit you'd want to be shooting fairly low for) then making your chunk sizes powers of two is not just ideal - it's essential.