Using bitwise operations for advanced checks and optimization
In most cases, a programmer will not need to worry too much about bits unless there is a need to write some compression algorithms, and when we are making a game, we never know when a situation such as that arises. In order to encode and decode files compressed in this manner, you need to actually extract data at the bit level. Finally, you can use bit operations to speed up your program or perform neat tricks. However, this is not always recommended.
Getting ready
For this recipe, you will need a Windows machine with a working copy of Visual Studio.
How to do it…
In this recipe, we will see how easy it is to use bitwise operations to perform operations by manipulating memory. Bitwise operations are also a great way to optimize code by directly interacting with memory:
- Open Visual Studio.
- Create a new C++ project.
- Add a source file called
main.cpp
or anything that you want to name the source file. - Add the following lines of code:
#include <iostream> #include <conio.h> using namespace std; void Multi_By_Power_2(int iNumber, int iPower); void BitwiseAnd(int iNumber, int iNumber2); void BitwiseOr(int iNumber, int iNumber2); void Complement(int iNumber4); void BitwiseXOR(int iNumber,int iNumber2); int main() { int iNumber = 4, iNumber2 = 3; int iPower = 2; unsigned int iNumber4 = 8; Multi_By_Power_2(iNumber, iPower); BitwiseAnd(iNumber,iNumber2); BitwiseOr(iNumber, iNumber2); BitwiseXOR(iNumber,iNumber2); Complement(iNumber4); _getch(); return 0; } void Multi_By_Power_2(int iNumber, int iPower) { cout << "Result is :" << (iNumber << iPower)<<endl; } void BitwiseAnd(int iNumber, int iNumber2) { cout << "Result is :" << (iNumber & iNumber2) << endl; } void BitwiseOr(int iNumber, int iNumber2) { cout << "Result is :" << (iNumber | iNumber2) << endl; } void Complement(int iNumber4) { cout << "Result is :" << ~iNumber4 << endl; } void BitwiseXOR(int iNumber,int iNumber2) { cout << "Result is :" << (iNumber^iNumber2) << endl; }
How it works…
The left shift operator is the equivalent of moving all the bits of a number a specified number of places to the left. In our example, the numbers we are sending to the function Multi_By_Power_2
is 4
and 3
. The binary representation of 4
is 100
, so if we shift the most significant bit, which is 1
, three places to the left, we get 10000
, which is the binary of 16
. Hence, left shift is equivalent to integer division by 2^shift_arg
, that is, 4*2^3
, which is again 16
. Similarly, the right shift operation is equivalent to integer division by 2^shift_arg
.
Now let us consider we want to pack data so that the data is compressed. Consider the following example:
int totalammo,type,rounds;
We are storing the total bullets in a gun; the type of gun, but it can only be a rifle or pistol; and the total bullets per round it can fire. Currently we are using three integer values to store the data. However, we can compress all the preceding data into one single integer and hence compress the data:
int packaged_data; packaged_data = (totalammo << 8) | (type << 7) | rounds;
If we assume the following notations:
- TotalAmmon:
A
- Type:
T
- Rounds:
R
The final representation in the data would be something like this:
AAAAAAATRRRRRRR