Java Reference
InDepth Information
When used with
boolean
operands,

is the infrequently used Boolean OR
operator described earlier.
Bitwise XOR (
^
)
This operator combines its two integer operands by performing a Boolean
XOR (exclusive OR) operation on their individual bits. The result has a bit set
if the corresponding bits in the two operands are different. If the correspondā
ing operand bits are both 1s or both 0s, the result bit is a 0. For example:
10
^
7
// 00001010 ^ 00000111 = => 00001101 or 13
When used with
boolean
operands,
^
is the seldom used Boolean XOR
operator.
Let shit (
<<
)
The
<<
operator shifts the bits of the left operand left by the number of places
specified by the right operand. Highorder bits of the left operand are lost, and
zero bits are shifted in from the right. Shifting an integer left by
n
places is
equivalent to multiplying that number by 2
n
. For example:
10
<<
1
// 00001010 << 1 = 00010100 = 20 = 10*2
7
<<
3
// 00000111 << 3 = 00111000 = 56 = 7*8

1
<<
2
// 0xFFFFFFFF << 2 = 0xFFFFFFFC = 4 = 1*4
If the left operand is a
long
, the right operand should be between 0 and 63.
Otherwise, the left operand is taken to be an
int
, and the right operand should
be between 0 and 31.
Signed right shit (
>>
)
The
>>
operator shifts the bits of the left operand to the right by the number of
places specified by the right operand. The loworder bits of the left operand are
shifted away and are lost. The highorder bits shifted in are the same as the
original highorder bit of the left operand. In other words, if the left operand is
positive, 0s are shifted into the highorder bits. If the left operand is negative,
1s are shifted in instead. This technique is known as
sign extension
; it is used to
preserve the sign of the left operand. For example:
10
>>
1
// 00001010 >> 1 = 00000101 = 5 = 10/2
27
>>
3
// 00011011 >> 3 = 00000011 = 3 = 27/8

50
>>
2
// 11001110 >> 2 = 11110011 = 13 != 50/4
If the left operand is positive and the right operand is
n
, the
>>
operator is the
same as integer division by 2
n
.
Unsigned right shit (
>>>
)
This operator is like the
>>
operator, except that it always shifts zeros into the
highorder bits of the result, regardless of the sign of the lefthand operand.
This technique is called
zero extension
; it is appropriate when the left operand
is being treated as an unsigned value (despite the fact that Java integer types are
all signed). These are examples: