Game Development Reference
In-Depth Information
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int first = 0x0F;
unsigned int second = 0x18;
unsigned int anded = first & second;
cout << hex << showbase;
cout << first << endl;
cout << second << endl;
cout << anded << endl;
return 0;
}
Note
We are using unsigned
int
and not unsigned
char
in these examples as
cout
prints character
symbols when using
char
types but converts to human-readable numbers when using
int
types.
We are using the
&
operator on the variables
first
and
second
in Listing 3-12 and storing the result
in
anded
. The first use of
cout
tells the output stream that we would like to show numbers in the hex
format. The showbase specifier tells the output stream that we would also like to show the 0x part of
the number. The output from this program would look like the following:
0xf
0x18
0x8
0x0f and 0x18 were our input numbers and 0x08 is the output generated. Table
3-2
shows how we
derive this result.
Table 3-2.
The Result of Using Bitwise & on 0x0F and 0x18
128
64
32
16
8
4
2
1
first
0
0
0
0
1
1
1
1
second
0
0
0
1
1
0
0
0
anded
0
0
0
0
1
0
0
0
Table
3-2
shows how we can derive 0x08 as the result of an
&
between 0x0F and 0x18. We begin
with the right-most column and work our way to the left.
In the 1s column we have a 1 in first and a 0 in second; therefore the result bit is 0.
In the 2s column we have a 1 in first and a 0 in second; therefore the result bit is 0.
In the 4s column we have a 1 in first and a 0 in second; therefore the result bit is 0.