Friday, November 4, 2011

Bitwise OR Operator in C programming

Another important bitwise operator is the OR operator which is represented as |. The rules that govern the value of the resulting bit obtained after ORing of two bits is shown in the truth table below.







Using the Truth table confirm the result obtained on ORing the two operands as shown below.
11010000 Original bit pattern
00000111 OR mask
-------------
11010111 Resulting bit pattern
Bitwise OR operator is usually used to put ON a particular bit in a number.
Let us consider the bit pattern 11000011. If we want to put ON bit number 3, then the OR mask to be used would be 00001000. Note that all the other bits in the mask are set to 0 and only the bit, which we want to set ON in the resulting value is set to 1.

No comments:

Post a Comment