左移(<<)就是将左边操作数在内存中的二进制数据左移右边操作指定的位数,右边移空的部分补0。右移(>>)如果最高位是0,左边移空的高位就填入0,如果最高位是1,左边移空的高位填入1。新的移位运算符(>>>),不管最高位是0还是1,左边移位的空位都填入0
移位运算符适应数据类型有:byte,short,char,int,long
1. 1.对低于int型的操作数将先自动转换为int型再移位。
2. 2.对于int型数移位a>>b,系统先将b对32取模,得到结果才是真正移位的位数.
3. 3.对于long型整型数移位时a>>b,先将移位位数b对64取模。
移位能为我们实现整数除以或乘以2的n次的效果,如果:x>>1的结果和x/2的结果是一样的,x<<2和x*4的结果是一样的。总之,一个数左移n位,就是等于这个树乘以2的n次方,一个数右移n位,就是等于这个数除以2的n次方.。
思考:屏幕上每个像素的颜色用一个8位的二进制数据表示,那么屏幕上最多少种颜色?
这其实就是如何用程序实现求2的x次方的问题。(Y=1<<x)
注意:移位不会改变变量本身的值。如a>>1;在一行语句中单独存在,毫无意义,因为它没有改变a的值,也没有把它赋值给别的变量。作者在三更半夜编程时,常犯这错误,也帮不少人排队过类似的错误。
SCJP考试笔记
Shift operators: Shift operators are used on integral numbers only (not float numbers).
>> right shift
<< left shift
>>> right shift including negative bit
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
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);
}
}
output:
Before shift x equals 8
After shift x equals 4
The following code uses a hexadecimal number to shift:
class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println(“Before shift x equals “ + x);
x = x << 1;
System.out.println(“After shift x equals “ + x);
}
}
it is necessary to convert the hexadecimal number to a bit number.
8 0 0 0 0 0 0 0
1000 0000 0000 0000 0000 0000 0000 0000
output:
Before shift x equals -2147483648
After shift x equals 0
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 0x80000000 again:
1000 0000 0000 0000 0000 0000 0000 0000
1100 0000 0000 0000 0000 0000 0000 0000
class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println("Before shift x equals " + x);
x = x >> 4;
System.out.println("After shift x equals " + x);
}
}
The number now equals the following in bit representation:
1000 0000 0000 0000 0000 0000 0000 0000
1111 1000 0000 0000 0000 0000 0000 0000
We can use a special shift operator if we do not want to keep the negative bit. This is the operator >>>. Let’s change the code slightly to use this operator:
class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println("Before shift x equals " + x);
x >>>= 4; //Assignment operator
System.out.println("After shift x equals " + x);
}
}
1000 0000 0000 0000 0000 0000 0000 0000
0000 1000 0000 0000 0000 0000 0000 0000
output:
Before shift x equals -2147483648
After shift x equals 134217728
Exercise 3-1
class BitShift {
public static void main(String [] args) {
int x = 0x00000001; // or simply 1
x <<= 31;
x >>= 31;
System.out.println("After shift x equals " + x);
}
}
output:
1111 1111 1111 1111 1111 1111 1111 1111