Page 34 - 6437
P. 34
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable ‘A’ holds
60 and variable ‘B’ holds 13, then:
Oper Description Example
ator
& Binary AND Operator copies a bit to the result (A & B) = 12, i.e.,
if it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e., 0011
either operand. 1101
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e., 0011
in one operand but not both. 0001
~ Binary Ones Complement Operator is unary (~A ) = -61, i.e., 1100
and has the effect of 'flipping' bits. 0011 in 2's
complement form.
<< Binary Left Shift Operator. The left operands A << 2 = 240,
value is moved left by the number of bits specified by i.e., 1111 0000
the right operand.
>> Binary Right Shift Operator. The left operands A >> 2 = 15, i.e.,
value is moved right by the number of bits specified by 0000 1111
the right operand.
Example
Try the following example to understand all the bitwise operators available in C:
#include <stdio.h>
main()
{
36