Bitwise operators
The bitwise operators allow you to
manipulate individual bits in a number (since floating point values use a
special internal format, the bitwise operators work only with integral types:
char, int and long). Bitwise operators perform Boolean
algebra on the corresponding bits in the arguments to
produce the result.
The bitwise
and operator
(&) produces a one in
the output bit if both input bits are one; otherwise it produces a zero. The
bitwise or operator
(|) produces a one in the
output bit if either input bit is a one and produces a zero only if both input
bits are zero. The bitwise exclusive or, or xor
(^)
produces a one in the output bit if one or the other input bit is a one, but not
both. The bitwise not
(~, also called the ones complement
operator) is a unary
operator – it only takes one
argument (all other bitwise operators are binary
operators). Bitwise not
produces the opposite of the input bit – a one if the input bit is zero, a
zero if the input bit is one.
Bitwise operators can be combined with
the = sign to unite the operation and assignment:
&=, |=, and
^= are all legitimate operations (since ~
is a unary operator it cannot be combined with the =
sign).