Game Development Reference
In-Depth Information
<< " or equal to your second."
<< endl;
return 0;
}
So far we have looked at arithmetic operators for carrying out basic mathematical operations and
relational operators for comparing the values stored in variables. The next set of operators supplied
by C++ is the bitwise operators, which allow us to modify the individual bits that make up our values.
Bitwise Operators
All of the values we have seen so far have been backed by bits stored in computer memory.
In a binary computer system, a single bit can either contain a one or a zero. We represent higher
numbers than these by using different patterns of bits. This section covers the operators that allow
us to manipulate bits.
As a programmer you will generally deal with data sizes in terms of numbers of bytes rather than
numbers of bits. A byte is a collection of 8 bits. We have already seen that the char data type
consists of a single byte and that an int consists of 4 bytes. This means that a char consists of 8
bits and an int is 32 bits.
We can think of the 8 bits of a byte as being represented in the same manner as in Table 3-1 .
Table 3-1. The Unsigned char Bit Sequence for 137
128
64
32
16
8
4
2
1
1
0
0
0
1
0
0
1
The bit sequence in Table 3-1 represents the unsigned char value of 137. We have a bit telling us
that the value consists of a 1, an 8, and a 128. These are added together to give us 137.
The bitwise operators allow us to set and unset individual bits, test if bits are set, and even merge bit
patterns together. A set bit means that its value is 1 and an unset bit has the value 0. The rest of this
section looks at the bitwise operators available in C++ as well as the hexadecimal representation of
numbers.
Hexadecimal Number Representation
When we normally write numbers, such as 11, we are using the decimal number representation.
Decimal numbers have a base 10 format. We say it is base 10, as we have 10 unique digits,
0 through 9. Once we add one to 9 in a column, we reset the column to 0 and add a one to the
column to the left, so 9 becomes 10, 19 becomes 20, and 1999 becomes 2000.
 
 
Search WWH ::




Custom Search