The bitwise inclusive OR operator is used to set specific bits to 1 in an operand. In Fig. 20.8, line 22 assigns 15
(00000000 00000000 00000000 00001111
) to variable number1
, and line 23 assigns 241
(00000000 00000000 00000000 11110001
) to variable setBits
. When number1
and setBits
are combined using the bitwise inclusive OR operator in the expression number1 | setBits
(line 28), the result is 255
(00000000 00000000 00000000 11111111
). Figure 20.9 summarizes the results of combining two bits with the bitwise inclusive-OR operator.
Common Programming Error 20.3
Using the logical OR operator (||
) for the bitwise OR operator (|
) and vice versa is a logic error.