Game Of Bits

Published by

on

Unlike the base 10 counting system ( 5 in the mind and 6 in hand… after 5… 😉 ) that we all have been using , base 2 system holds a special place in the world of computers. I admire their simplicity when it comes to game development. Jotting down a bunch of use cases related to Game Development and the world of Unity Engine.

Before we dive into Bits , let’s clear out some basic facts. SHORT <= INT , ideally in any compiler implementation. In other words the following holds true in modern machines and compilers .

CPU             short   int
8 bit           16      16
16 bit          16      16
32 bit          16      32
64 bit          16      32

For an in depth explanation of all possible bitwise operations, refer wiki

Consider a Multiplayer RPG game which requires us to keep track of all the attributes of a hero , say sword , magic spell , armour , skull cap etc to be tracked. For simplicity let’s consider a SHORT.

DecimalBinary
160000 0000 0001 0000
80000 0000 0000 1000
40000 0000 0000 0100
20000 0000 0000 0010
10000 0000 0000 0001

Coming back to the RPG game we were speaking about , The following code will try to explain the use of bitwise operators and make like a bit easier 🙂 .

//For explanation only 
public static short SWORD = 16;
public static short SHIELD = 8;
public static short SKULLCAP = 4;
public static short ARMOUR = 2;
public static short BOOTS = 1;

// usually games make use of Long to store more
// For simplicity and writing less , lets use short
short INVENTORY = 0; // Bits : 0000 0000 0000 0000

//Only BOOTS available when the game starts
INVENTORY |= BOOTS; 

//INVENTORY Bits : 0000 0000 0000 0001

//As the game progresses our Hero collects an armour
//Add armour to the list of INVENTORY
INVENTORY |= ARMOUR; 

//INVENTORY Bits: 0000 0000 0000 0011

//Fights a tiny boss to collect a shiny sword and a shield
//Add both to the inventory
INVENTORY |= (SWORD | SHIELD ); 

//INVENTORY Bits: 0000 0000 0001 1011

//Fights a bunch of wolves and loses his Armour in the process
INVENTORY &= ~ARMOUR; 

//INVENTORY Bits: 0000 0000 0001 1001 

Why not use boolean instead of the bitwise operators in the above code snippet?

  1. Hard to manage booleans , imagine an inventory of 15-20 items. The number of booleans would get hard to manage and messy.
  2. As the number of heroes and bots in your game grows, the memory used will be far greater than the use of a LONG or INT datatype.

Another valuable use case of bitwise operators are in board games through bitBoards. Great explanation for the same available in the following video !

Bitboards for easier state tracking in Board Games

In case of Unity and other game engine. Bit masks are used to enable collision detection and raycasting in the physics world. Read about it here.

Unity 3D game engine LayerMask

Hope this article has helped you understand the basics of bitwise operators. Happy Coding !

Reference:
AnIntroduction to Game Math – An excellent article on bitwise operation in Games