Game Development Reference
In-Depth Information
In the 8s column we have a 1 in first and a 1 in second; therefore the result bit is 1.
In the 16s column we have a 0 in first and a 1 in second; therefore the result bit is 0.
All other columns have 0s in both values.
As you can see, to be able to have a 1 in a bit column in our result value the corresponding bit
column also had to be 1 in the first
and
second values. If either of the values was a 0 then the result
bit at the same column is also a 0.
The Binary | (OR) Operator
The Bitwise
|
, or OR, operator is used to determine if either of the bits in a given column is set to
1 in either of the two supplied values. Table
3-3
shows the result of using
|
with the same 0x0F and
0x18 values as the previous example.
Table 3-3.
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
ord
0
0
0
1
1
1
1
1
The result of the
|
on the two values is 0x1F. We derived this using the following method from
left to right.
In the 1s column we have a 1 in first; therefore the result bit is 1.
In the 2s column we have a 1 in first; therefore the result bit is 1.
In the 4s column we have a 1 in first; therefore the result bit is 1.
In the 8s column we have a 1 in first; therefore the result bit is 1.
In the 16s column we have a 1 in second; therefore the result bit is 1.
All other columns have 0s in both values.
Using the
|
operator is as simple as using the
&
operator; however, the resulting bit for each column
is set to 1 if either of the bits from the two input values were 1.
The Binary ^ (Exclusive OR) Operator
Our last bitwise operator is the
^
(or Exclusive OR/XOR) operator. This operator differs from the |
operator by requiring that one of the two input values bits is set, but not both. Table
3-4
shows this
in practice.