# Any advantage of having chunks with sizes by the power of two?

With my past experience of having my little game lag because of the size of the world, I have decided that in whatever next project I might choose to create, I will split said world into chunks. Now here comes my question : Should the chunk's width, height and depth be a power of two?

For example, in a little game called Minecraft, chunks have a height of 256 and width,depth of 16. Both numbers a power of two. Is there any advantage over having the chunk simply be 100x100x100?

Minecraft was written in Java, but I'm working in Lua. Just pointing that out in case it has any relevance.

The main advantage of power-of-two sizes is that division by a power of 2 (on an integer) is just a bit shift, and modding is just a mask. Both of these operations are blazingly fast to do on gobs of numbers, while division by an arbitrary value can be substantially slower.

So if I want to quickly find which cell of which chunk a particular point is in, I can write something like this for each axis:

rounded = RoundToInt(axisValue);

cell = rounded & 0xFF;
chunk = rounded >> 8;


If you're using image tiles for each chunk anywhere (like hightmap / splatmap / biome encoding...), GPUs also like to work with power-of-two-sized images, and this might let you use built-in mipmapping hardware for diminishing the level of detail in the distance, without extra padding/adjustments.

In some cases you might also get better alignment of your data in memory to 32-byte / page boundaries when using power-of-two sized chunks, but this is likely to be a much more minor effect.

• I dont think I can explain it well so ill leave it to you. Dont forget about octrees and quadtrees when it comes to collision checking and data retrieval – DarkDestry Dec 30 '18 at 17:06
• I think that would make a great answer to add, @DarkDestry. :) – DMGregory Dec 30 '18 at 17:08

What I think the primary reason why tile based games like power of 2 is due to the fact that they store world data in octrees and quadtrees.

Using a 2D example, spatial partitioning techniques such as quadtrees will allow for highly optimized collision and raytracing checks. Quadtrees essentially divide each chunk into 4 equal quadrant each time, and items that lie entirely in each quadrant is guaranteed to not be able to collide with items in other quadrants at all, thus significantly cutting down on the amount of collision checks you need to perform.

Octrees are an extension of the 2D example in 3D space, except that each cube is divided into 8 parts.