Game Development Reference
In-Depth Information
Note It's probably not a coincidence that we have 10 fingers, or digits, on our hands and that our modern
counting systems are all based on the base 10 number system. The binary numbers we have been looking at
so far are actually numbers in the base 2 representation of numbers!
Table 3-1 shows how we can represent numbers in the binary format. 1 is 1, 2 in binary is 10, and 137
in binary is 10001001. Once we get to 32-bit values we have to keep track of 32 individual bits, and
things are even worse now that we are seeing processors that can operate on 64-bit values. To make
life easier for all, the hexadecimal number format can be used to represent large bitfields. Despite the
“hex” part leading us toward believing that we are dealing with sets of six, hexadecimal values actually
work in sets of 16, or base 16. The hexadecimal representation uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
A, B, C, D, E, and F where A, B, C, D, E, and F represent 10, 11, 12, 13, 14, and 15, respectively.
Table 3-1 can be used to work out that the binary value 1111 is actually the number 15. We can
represent the 8-bit value of 15 in C++ using the following line of code:
unsigned char hex15 = 0x0F;
If we wish to print a value out onto the console in hex format we tell cout in the following manner:
cout << std::hex << number << std::dec << number;
For the rest of this section we use simple 8-bit binary representations of numbers to show how the
bitwise operators change values in our code. This small section on hexadecimal numbers is useful,
as we would use this format for binary values in source code for shipping game titles.
C++ provides us with the following bitwise operators:
AND operator
OR operator
Exclusive OR operator
Left Shift operator
Right Shift operator
The Binary & (AND) Operator
The & operator is known as the AND operator. Listing 3-12 shows how the & operator is used.
Listing 3-12. The Bitwise & Operator
#include "stdafx.h"
#include <iostream>
using namespace std;
 
Search WWH ::




Custom Search