Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C++ Game Development Cookbook

You're reading from   C++ Game Development Cookbook

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785882722
Length 346 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Druhin Mukherjee Druhin Mukherjee
Author Profile Icon Druhin Mukherjee
Druhin Mukherjee
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Game Development Basics FREE CHAPTER 2. Object-Oriented Approach and Design in Games 3. Data Structures in Game Development 4. Algorithms for Game Development 5. Event-Driven Programming – Making Your First 2D Game 6. Design Patterns for Game Development 7. Organizing and Backing Up 8. AI in Game Development 9. Physics in Game Development 10. Multithreading in Game Development 11. Networking in Game Development 12. Audio in Game Development 13. Tips and Tricks Index

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:

  1. Open Visual Studio.
  2. Create a new C++ project.
  3. Add a source file called main.cpp or anything that you want to name the source file.
  4. 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
You have been reading a chapter from
C++ Game Development Cookbook
Published in: May 2016
Publisher: Packt
ISBN-13: 9781785882722
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime