Bitwise Inclusive OR Operator (|)

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.

Image

Fig. 20.9. Combining two bits with the bitwise inclusive-OR operator (|).


Image Common Programming Error 20.3

Using the logical OR operator (||) for the bitwise OR operator (|) and vice versa is a logic error.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.67