Bitwise operators compare each corresponding bit in the expressions
and produce a result based upon that comparison. Bitwise operators
apply only to integer type expressions.
Table 1-6: Bitwise operators
Operator
|
Description
|
Example
|
&
|
Bitwise AND.
Sets the bit if both bits are set.
|
c = a &
b;
The bitwise value of “c” is 0010 0010.
|
&=
|
Bitwise AND and assign.
This is the same as a = a &
b; .
|
a &= b;
The new bitwise value of “a” is 0010 0010.
|
|
|
Bitwise OR.
Sets the bit if either bit is set.
|
c = a | b;
The bitwise value of “c” is 1011 1111.
|
|=
|
Bitwise OR and assign.
This is the same as a = a | b;.
|
a |= b;
The new bitwise value of “a” is 1011 1111.
|
^
|
Bitwise exclusive OR.
Sets the bit when only one of the bits is set.
|
c = a ^ b;
The bitwise value of “c” is 1001 1101.
|
^=
|
Bitwise exclusive OR and
assign.
This is the same as a = a ^ b; .
|
a ^= b;
The new bitwise value of “a” is 1001 1101.
|
~
|
1’s compliment.
Reverses the setting of each bit.
|
c = ~b;
The bitwise value of “c” is 1100 0100.
|