The output of this program follows:
a
=
2
b
=
3
c
=
4
d
=
1
The Bitwise Operators
Java defines several bitwise operators that can be applied to the integer types, long, int, short,
char, and byte. These operators act upon the individual bits of their operands. They are
summarized in the following table:
Operator
Result
~
Bitwise unar y NOT
&
Bitwise AND

Bitwise OR
^
Bitwise exclusive OR
>>
Shift right
>>>
Shift right zero fill
<<
Shift left
&=
Bitwise AND assignment
=
Bitwise OR assignment
^=
Bitwise exclusive OR assignment
>>=
Shift right assignment
>>>=
Shift right zero fill assignment
<<=
Shift left assignment
Since the bitwise operators manipulate the bits within an integer, it is important to
understand what effects such manipulations may have on a value. Specifically, it is useful
to know how Java stores integer values and how it represents negative numbers. So, before
continuing, let's briefly review these two topics.
All of the integer types are represented by binary numbers of varying bit widths. For
example, the byte value for 42 in binary is 00101010, where each position represents a power
of two, starting with 20 at the rightmost bit. The next bit position to the left would be 21, or 2,
continuing toward the left with 22, or 4, then 8, 16, 32, and so on. So 42 has 1 bits set at positions
1
3
5
1, 3, and 5 (counting from 0 at the right); thus, 42 is the sum of 2 + 2 + 2 , which is 2 + 8 + 32.
All of the integer types (except char) are signed integers. This means that they can represent
negative values as well as positive ones. Java uses an encoding known as two's complement,
which means that negative numbers are represented by inverting (changing 1's to 0's and
vice versa) all of the bits in a value, then adding 1 to the result. For example, 42 is represented
by inverting all of the bits in 42, or 00101010, which yields 11010101, then adding 1, which
results in 11010110, or 42. To decode a negative number, first invert all of the bits, then add 1.
For example, 42, or 11010110 inverted, yields 00101001, or 41, so when you add 1 you get 42.
Search WWH :