Java Reference
In-Depth Information
The value of 13 | 3 can be computed as follows. The result of
13 | 3
is
15
.
13 00000000 00000000 00000000 00001101
3 00000000 00000000 00000000 00000011
--------------------------------------------
13 | 3 00000000 00000000 00000000 00001111 (Equal to decimal 15)
The bitwise
XOR
(
^
) operates on corresponding bits of its operands and returns 1 if only one of the bits is 1.
Otherwise, it returns 0. The following is the result of all bit combinations using bitwise
XOR
(
^
) operator:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
The value of
13 ^ 3
can be computed as follows. The result of
13 ^ 3
is
14
.
13 00000000 00000000 00000000 00001101
3 00000000 00000000 00000000 00000011
-------------------------------------------
13 ^ 3 00000000 00000000 00000000 00001110 (Equal to decimal 14)
The bitwise
NOT
(
~
) operates on each bit of its operand. It inverts the bits, that is, 1 is changed to 0 and 0 is
changed to 1. It is also called a bitwise complement operator. It computes 1's complement of its operand. The
following is the result of all bit combinations using bitwise
NOT
(
~
) operator:
~1 = 0
~0 = 1
The value of
~13
can be computed as follows. The result of
~13
is
-14
.
13 00000000 00000000 00000000 00001101
------------------------------------------
~13 11111111 11111111 11111111 11110010 (Equal to decimal -14)
The bitwise left shift operator (<<) shifts all the bits to the left by the number of bits specified as its right-hand
operand. It inserts zeros at the lower-order bits. The effect of shifting 1 bit to left is same as multiplying the number by 2.
Therefore, 9 << 1 will produce 18, whereas 9 << 2 produces 36. The procedure to compute 13 << 4 can be depicted
as shown in Figure
4-1
.
Figure 4-1.
Computing 13 << 4