|
|
Bit shift operators
The bit shift operators shift the bits in an integer type by a specified number of places to the right or left. Bit shift operators cannot be used on floats, doubles, or booleans. For example, << is the left shift operator. The integer 78 is 00000000000000000000000001001110 in binary. Table 2-6 shows the result of shifting it progressively leftward. Notice that at each step the pattern of ones and zeroes appears to move one bit further left.
Table 2-6 Left-shifting 78
|
Value
| Bit Pattern
|
|
78
| 00000000000000000000000001001110
|
78 << 1 = 156
| 00000000000000000000000010011100
|
78 << 2 = 312
| 00000000000000000000000100111000
|
78 << 3 = 624
| 00000000000000000000001001110000
|
78 << 4 = 1248
| 00000000000000000000010011100000
|
78 << 5 = 2496
| 00000000000000000000100111000000
|
78 << 6 = 4992
| 00000000000000000001001110000000
|
78 << 7 = 9984
| 00000000000000000010011100000000
|
78 << 8 = 19,968
| 00000000000000000100111000000000
|
78 << 9 = 39,936
| 00000000000000001001110000000000
|
78 << 10 = 79,872
| 00000000000000010011100000000000
|
78 << 11 = 159,744
| 00000000000000100111000000000000
|
78 << 12 = 319,488
| 00000000000001001110000000000000
|
78 << 13 = 638,976
| 00000000000010011100000000000000
|
|
Also notice that at each step, the value of the number is doubled. A 1-bit shift left is exactly equivalent to multiplication by two. Depending on the compiler, the virtual machine, and the CPU, it may be mildly quicker to shift an int to the left by the appropriate number of bits rather than to multiply by two. Similarly, shifting an int to the right can replace dividing by two or a power of two. However, this optimization may well not be worth the decrease in the legibility of your code, even on platforms where it makes a difference in performance.
What happens when the pattern of ones reaches the left side? Does it wrap around? No. The ones just march off to the left as the right side fills with zeroes. Note that once you hit 25 left shifts, you lose the multiplication by two property and drop over into negative numbers. If you had started with a larger number, this might have happened sooner. From that point on, the results bear little numerical relation to the original 78. Table 2-7 demonstrates.
Table 2-7 Left-shifting 78 by 22 to 31 places
|
Value
| Bit Pattern
|
|
78 << 22 = 327,155,712
| 00010011100000000000000000000000
|
78 << 23 = 654,311,424
| 00100111000000000000000000000000
|
78 << 24 = 1,308,622,848
| 01001110000000000000000000000000
|
78 << 25 = 1,677,721,600
| 10011100000000000000000000000000
|
78 << 26 = 939,524,096
| 00111000000000000000000000000000
|
78 << 27 = 1,879,048,192
| 01110000000000000000000000000000
|
78 << 28 = -536,870,912
| 11100000000000000000000000000000
|
78 << 29 = -1,073,741,824
| 11000000000000000000000000000000
|
78 << 30 = -2,147,483,648
| 10000000000000000000000000000000
|
78 << 31 = 0
| 00000000000000000000000000000000
|
|
However, if you keep going, something interesting happens. The next shift, by 32, appears to bring the number back, as Table 2-8 demonstrates.
Table 2-8 should look familiar. Except for the number of bits by which 78 is shifted, its an exact copy of Table 2-6. Did it just take a little extra time to wrap around? Not exactly. Java limits the right side of the shift operator to five bits (six bits if the left side is a long). Extra bits are truncated. This means that you can only really shift an int (or a byte, or a short) between 0 and 31 bits. Longs can be shifted between 0 and 63 bits. If you try to shift by more than that, Java throws away the higher-order bits. Thus, in the last line of Table 2-8, 78 is really being shifted by 45 - 32 = 13 bits, not by 45 bits.
Table 2-8 Left-shifting 78 by values greater than 31
|
Value
| Bit pattern
|
|
78 << 32 = 78
| 00000000000000000000000001001110
|
78 << 33 = 156
| 00000000000000000000000010011100
|
78 << 34 = 312
| 00000000000000000000000100111000
|
78 << 35 = 624
| 00000000000000000000001001110000
|
78 << 36 = 1248
| 00000000000000000000010011100000
|
78 << 37 = 2496
| 00000000000000000000100111000000
|
78 << 38 = 4992
| 00000000000000000001001110000000
|
78 << 39 = 9984
| 00000000000000000010011100000000
|
78 << 40 = 19,968
| 00000000000000000100111000000000
|
78 << 41 = 39,936
| 00000000000000001001110000000000
|
78 << 42 = 79,872
| 00000000000000010011100000000000
|
78 << 43 = 159,744
| 00000000000000100111000000000000
|
78 << 44 = 319,488
| 00000000000001001110000000000000
|
78 << 45 = 638,976
| 00000000000010011100000000000000
|
|
|