Many years ago, I taught myself game development from Andre LaMothe’s “Windows Game Programming For Dummies”. Back then, I was just learning about C/C++, Win32 and DirectX for making video games.
One of the interesting things I learned was about using bitwise operators to store multiple states in a single variable. This can still be useful in any C-derived language, such as C#. You can even use this technique in JavaScript!
Let’s say, your game character has a carry-on weapon bag with 8 available slots. Each slot can store 1 type of weapon.
You can then store a flag in each bit of an 8-bit integer to indicate whether that weapon is in the bag or not. In fact, you can use a 16-bit integer to store 16 states if necessary.
- Use a constant integer to store each weapon in powers of 2 (e.g. 1, 2, 4, etc)
- Use an integer variable to represent the storage bag.
- Use the binary OR operator | to set one or more bits.
- Use the binary AND operator & to test each bit.
For simplicity, Step 1 is shown as hard-coded assignments. However, you can use bit-shift operators to set each value.
e.g. 1 << 0 = 1; 1 << 1 = 2; 1 << 2 = 4, etc
To learn more about bitwise operators and how they work, check out these articles:
Bitwise operators in c# OR(|), XOR(^), AND(&), NOT(~)
http://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.htmlImran Nazar: Bitwise Operators
http://imrannazar.com/Bitwise-Operators
Here’s the code for a Console Application in C#:
// define weapons const int knife = 1; const int pistol = 2; const int sword = 4; const int stick = 8; const int shotgun = 16; Console.WriteLine("Weapons defined: knife, pistol, sword, stick, shotgun"); // create weapon bag int myWeaponBag; Console.WriteLine("Initiating weapon bag..."); // store weapons in bag Console.WriteLine("Storing a few weapons in bag (knife, pistol, stick)..."); myWeaponBag = knife | pistol | stick; // check for weapons Console.WriteLine("Looking for weapons..."); Console.WriteLine((myWeaponBag & knife) > 0 ? "--> Found knife" : "NO knife"); Console.WriteLine((myWeaponBag & pistol) > 0 ? "--> Found pistol" : "NO pistol"); Console.WriteLine((myWeaponBag & sword) > 0 ? "--> Found sword" : "NO sword"); Console.WriteLine((myWeaponBag & stick) > 0 ? "--> Found stick" : "NO stick"); Console.WriteLine((myWeaponBag & shotgun) > 0 ? "--> Found shotgun" : "NO shotgun");
The results are shown below:
Weapons defined: knife, pistol, sword, stick, shotgun Initiating weapon bag... Storing a few weapons in bag (knife, pistol, stick)... Looking for weapons... --> Found knife --> Found pistol NO sword --> Found stick NO shotgun
There are many different ways of getting the same results: you could define a class for the weapon bag itself, use enumerators for the constants, etc. These solutions are left up to you, the reader. In the meantime, I hope you enjoyed this alternative method of using bitwise operators to store multiple booleans in a single int.
Nicely explained.. Please visit my blog to go through Bitwise AND. https://csparadigms.blogspot.in/2017/11/bitwise-and-for-multiple-numbers.html
Loved it…. Awesome explanation ….After a huge surfing finally i found the real time example.