java的移位运算

左移(<<)就是将左边操作数在内存中的二进制数据左移右边操作指定的位数,右边移空的部分补0。右移(>>)如果最高位是0,左边移空的高位就填入0,如果最高位是1,左边移空的高位填入1。新的移位运算符(>>>),不管最高位是0还是1,左边移位的空位都填入0


移位运算符适应数据类型有:byte,short,char,int,long

1. 1.对低于int型的操作数将先自动转换为int型再移位。

2. 2.对于int型数移位a>>b,系统先将b32取模,得到结果才是真正移位的位数.

3. 3.对于long型整型数移位时a>>b,先将移位位数b64取模。

移位能为我们实现整数除以或乘以2n次的效果,如果:x>>1的结果和x/2的结果是一样的,x<<2x*4的结果是一样的。总之,一个数左移n位,就是等于这个树乘以2n次方,一个数右移n位,就是等于这个数除以2n次方.

思考:屏幕上每个像素的颜色用一个8位的二进制数据表示,那么屏幕上最多少种颜色?

这其实就是如何用程序实现求2x次方的问题。(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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值