r/AskComputerScience • u/FigureOfStickman • Apr 03 '25
Did Minecraft's use of base-2 numbers have some kind of benefit for a Java game? Or was it just for the aesthetic?
This is something I've been thinking about for years.
- Items in the player's inventory can stack up to 64
- Terrain is famously generated and stored in chunks of 16x16 blocks. (Slime chunks, land claiming plugins, 384-block build height, etc)
- All the default textures are 16x16 pixels for a block
- I can't think of other examples off the top of my head
But at the same time, the crafting grid has 9 slots. the inventory has 36. Chests and barrels are 27. Brewing stands only hold 3 potions, and hoppers have 5 item slots. Multiples of three, along with a random five. some of the most aesthetically haunting numbers.
I think some examples of base-2 numbering are clearly internal values that became documented and understood as game mechanics over the years. Then again, the redstone system (the game's adaptation of electricity and wiring) had logic gates before it had pistons and railroads. idk
54
u/randomnamecausefoo Apr 03 '25
You’re using the term “base-2” when what you really mean is “powers of 2”.
21
u/DTux5249 Apr 03 '25 edited Apr 03 '25
Java stores integers using 4 bytes regardless of how large the number is. The largest number they could store in any given number variable is
42949672952147483647 before they have to worry about storage requirements doubling.It's purely aesthetic. Likely because in a world full of cubes with 8 vertices, 64 would appear to be a round number. Same goes for the pixels of the sprites used. They chose 16×16 because it was a multiple of 8 that was square while being small enough of a subdivision to be useful while being noticeably blocky
4
u/strcspn Apr 03 '25
Yeah, it's arbitrary. Maybe chunks have a reason for being 16x16 if they are packed in some way but I doubt it. I guess if you want to force it a little bit there is a reason for the textures being 16x16 (OpenGL by default only supports textures with power of two dimensions, but you can disable that).
2
u/two_three_five_eigth Apr 03 '25
It's probably because the developers wanted it that way. It likely has nothing to do with base-2.
Textures always have to be a sized to a power of 2 (which 16 is), but they likely packed all the block textures into one big one, so even that is pretty tenuous.
Realistically no modern console/computer/phone is so resource limited they'd have to resort to bit-packing to save space. Developers are all very familiar with base-2 numbers, which is likely how those were chosen.
2
u/theLOLflashlight Apr 03 '25
There are many memory related benefits to keeping things in powers of two on computers, generally, not just for a java application. At the end of the day it means the game can run faster.
1
u/granadesnhorseshoes Apr 03 '25
Helps keeping alignment of structs and doing stuff like bitshift arithmetic, register values stored as ints, etc.
"slots" being odd numbers doesn't really matter as much because they are just that, slots for data, not data themselves.
Even then I bet there are reasons for those values, EG a chest struct is a fixed size, lets say 4kb, and after all the other data to define a chest is set, there is enough space left for 27 slots. A crafting tables grid is 9 slots? Funny how that is an array[0-8] hu?
1
u/ICantBelieveItsNotEC Apr 03 '25
At the time when Minecraft came out, GPUs pretty much required that textures were a power of 2 square. If you tried to use a non-power-of-2, non-square texture, either your OpenGL/DirectX calls to create the texture would fail completely, or sampling from those textures would be horrendously slow.
Even today, NPOT textures are generally avoided because they cause issues with mipmapping and downsampling. Square power-of-two textures have the nice property that you can downsample to half resolution by averaging the value of every square of four pixels in the texture.
1
u/PlasmaFarmer Apr 03 '25
Regarding textures, it's best practice to use power of 2, so 256x256 or 4096x4096, etc.. The 16x16 size is also power of 2 of minecraft textures and the small size I think is purely aesthetics choice.
1
u/MikemkPK Apr 03 '25
The textures aren't exactly arbitrary; they have to be a power of two for GPU compatibility. They didn't have to be 16x16 though. They could be 8x8, 32x32, ... They could even have the block corners mapped to the middle of the texture to only display any arbitrary number, but it would still be 2nx2n on disk and in memory, and at that point, you're just wasting pixels to achieve the desired size.
1
u/iamcleek Apr 03 '25
i don't know how Minecraft generates its terrain. but, powers of two are common in auto-generated terrain schemes because it's commonly done by repeatedly subdividing a plane in half then modifying the height of the new vertexes. so, you're going to end up with n^2 squares.
textures are powers of 2 because GPUs require it.
the other things are probably arbitrary based on game testing or even just screen size.
1
u/bearicorn Apr 03 '25
It’s aesthetic
1
u/etherealflaim Apr 04 '25
People are talking a lot about the size in memory for Java, but skipping over encodings. Saving worlds in a binary encoded way, transmitting them for networked worlds, and storing bitmaps of changed chunks can all be more efficient if you use powers of two. Whether they made use of these is less important than the fact that they left themselves this option. The more times something appears in a game world, the more you might want to have your optimization options kept open for the future.
1
u/airodonack Apr 04 '25
Yes.
Game engines generally use powers-of-2 in order to optimize memory access. Inside CPUs/GPUs, a ton of the channels move bytes around in units of powers-of-2. You don't see it for things like crafting grids, inventory, chests, and barrels because it's more important to balance for gameplay reasons and they have near-zero effect on performance. But when it comes to rendering textures or generating chunks, making things as fast as possible means you can get bigger maps/faster loading/more FPS.
1
u/userhwon Apr 04 '25
Notch started out designing it to use every bit efficiently, because he wanted to get as many objects into the game as possible.
After a while he started seeing where it wouldn't be too inefficient for some things not to use every bit completely. And there may be places he used the spare values for other things.
1
u/Isogash Apr 06 '25
It serves a purpose in that the stacks split more evenly, since the only original shortcut to quickly divide up stacks was a simple split, and being able to do this mattered for crafting.
1
u/cfehunter Apr 06 '25
It's not entirely arbitrary for everything. Powers of two let you do some neat binary tricks and low level optimisations.
Stacks being limited to 64 though. That's 100% just Notch being a programmer, powers of 2 just stick in our heads.
1
u/Jumpy-Dig5503 Apr 06 '25
Base 2 numbers in a computer program are no more mysterious than base 10 numbers in regular life. When’s the last time you wondered why someone talked about 10, 100, or 1000 of something. Same thing, literally.
1
u/unrelevantly Apr 07 '25
Think about scale. How many chunks does minecraft want to support? How many textures will minecraft want to support loading? How many possible stacks of items might exist?
These numbers are much more relevant and will occur much more often than the number of brewing stands or the number of chests.
There's no reason to use any texture size besides 88, 1616 or 32*32 because you will waste significant amounts of space for no reason. Same for item stack size.
The number of slots in a brewing stand or crafting table are entirely irrelevant. All the counter examples you gave are very limited UI elements. Inventories are stored in NBT files, which use a json style entry for each slot. The number of slots that can be supported is entirely arbitrary.
-2
u/Aggressive-Share-363 Apr 03 '25
It affects storage considerations. It takes an entire extra bit to store 65 vs 64. Which in the grand scheme of the game isn't a lot for something like inventory, but can be much more significant with thing like chunk size and block data. It's an optimization but not a particularly important one. A more relevant reason for gameplay may be how it splits in two, as that's a basic inventory management operation. With a max stack size being a power of two, you can split it into clean halves a maximal number of times, which in turn let's you recombine those parts to reach arbitrary stack sizes relatively easily. This also isn't super important.
But what would be a better number? 100 so it's nice and even? Then you have an extra digits to worry about in the UI. 99 to avoid the extra digits? Rhen it's not even an even number and can't be split evenly at all. Nothing horrible would happen to the game with a different number, but there are some mild advantages for these numbers over others.
Other numbers like the crafting grid and total number of slots in the inventory are more for the gameplay and UI considerations.