The following are shift operators:
· >> right shift
· << left shift
· >>> right shift including negative bit
The more obscure the topic, the more likely it will appear on the exam. Operators such as +, -, *, and / will probably not be on the exam because they are commonly used. Shift operators are rarely or never used; therefore, they will most definitely be tested.
The shift operators shift the bits of a number to the right or left, producing a new number. Shift operators are used on integral numbers only (not float numbers). To determine the result of a shift, it is necessary to convert the number into bits. Let’s look at an example of a bit shift, without using code.
We’ll start with a simple example. Let’s use the right-shift operator on the integer 8. First, we must convert this number to a bit representation:
0000 0000 0000 0000 0000 0000 0000 1000
An int is a 32-bit integer, so all 32 bits must be displayed. If we apply a bit shift of one to the right, using the >> operator, the new bit number is:
0000 0000 0000 0000 0000 0000 0000 0100
We can now convert this back to a decimal number (base 10), to get 4. Now let’s examine how to do the exact same thing with code:
class BitShift {
public static void main(String [] args) {
int x = 8;
System.out.println(”Before shift x equals ” + x);
x = x >> 1;
System.out.println(”After shift x equals ” + x);
}
}
When we compile and run this program we get the following output:
c:\Java Projects\BitShift>java BitShift
Before shift x equals 8
After shift x equals 4
As you can see, the results are exactly what we expected them to be. Shift operations can take place on all integral numbers, regardless of the base they are displayed in (octal, decimal, or hexadecimal). The left shift works in exactly the same way, except all bits are shifted in the opposite direction. The following code uses a hexadecimal number to shift:
class BitShift {
public static void main(String [] args) {
int x = 0×80000000;
System.out.println(“Before shift x equals “ + x);
x = x << 1;
System.out.println(“After shift x equals “ + x);
}
}
To understand the preceding example, it is necessary to convert the hexadecimal number to a bit number. Fortunately it is very easy to convert from hexadecimal to bits. Each hex-digit converts to a four-bit representation, as we can see:
8 0 0 0 0 0 0 0
1000 0000 0000 0000 0000 0000 0000 0000
In the preceding example, the very leftmost bit represents the sign (positive or negative). When the leftmost bit is 1 the number is negative, and when it is 0 the number is positive. Running our program gives us the following:
c:\Java Projects\BitShift>java BitShift
Before shift x equals -2147483648
After shift x equals 0
As we can see, shifting the bits one to the left moves the bit right out of the number, leaving us with all 0 bits.
When there is a negative bit and we shift to the right, the operator shifts the bit to the right but also keeps the negative bit. For example, let’s use the hex number 0×80000000 again:
1000 0000 0000 0000 0000 0000 0000 0000
Now we will shift the bits, using >>, one to the right:
1100 0000 0000 0000 0000 0000 0000 0000
As we can see, the bit is shifted to the right but (and this is important) the negative bit remains behind. Let’s try some code that shifts it four to the right:
