1、左移(>>)
符号位一起参加移位
对于int型,最大的移位数是31,当>=32时,对32取模然后移位;
对于long型,最大的移位数是62,当>=64时,对64取模然后移位;
观察一段代码:
import java.util.*; public class Test{ public static void main(String[] args){ int i = 0; for(i= -1 ; i != 0 ; i <<= 1){ System.out.println("i=" +i+":\t"+ Integer.toBinaryString(i)); } System.out.println("i=" +i+":\t"+ Integer.toBinaryString(i)); for(i= 1 ; i != 0 ; i <<= 1){ System.out.println("i=" +i+":\t"+ Integer.toBinaryString(i)); } System.out.println("i=" +i+":\t"+ Integer.toBinaryString(i)); } }
i=-1: 11111111111111111111111111111111 i=-2: 11111111111111111111111111111110 i=-4: 11111111111111111111111111111100 i=-8: 11111111111111111111111111111000 i=-16: 11111111111111111111111111110000 i=-32: 11111111111111111111111111100000 i=-64: 11111111111111111111111111000000 i=-128: 11111111111111111111111110000000 i=-256: 11111111111111111111111100000000 i=-512: 11111111111111111111111000000000 i=-1024: 11111111111111111111110000000000 i=-2048: 11111111111111111111100000000000 i=-4096: 11111111111111111111000000000000 i=-8192: 11111111111111111110000000000000 i=-16384: 11111111111111111100000000000000 i=-32768: 11111111111111111000000000000000 i=-65536: 11111111111111110000000000000000 i=-131072: 11111111111111100000000000000000 i=-262144: 11111111111111000000000000000000 i=-524288: 11111111111110000000000000000000 i=-1048576: 11111111111100000000000000000000 i=-2097152: 11111111111000000000000000000000 i=-4194304: 11111111110000000000000000000000 i=-8388608: 11111111100000000000000000000000 i=-16777216: 11111111000000000000000000000000 i=-33554432: 11111110000000000000000000000000 i=-67108864: 11111100000000000000000000000000 i=-134217728: 11111000000000000000000000000000 i=-268435456: 11110000000000000000000000000000 i=-536870912: 11100000000000000000000000000000 i=-1073741824: 11000000000000000000000000000000 i=-2147483648: 10000000000000000000000000000000 i=0: 0 i=1: 1 i=2: 10 i=4: 100 i=8: 1000 i=16: 10000 i=32: 100000 i=64: 1000000 i=128: 10000000 i=256: 100000000 i=512: 1000000000 i=1024: 10000000000 i=2048: 100000000000 i=4096: 1000000000000 i=8192: 10000000000000 i=16384: 100000000000000 i=32768: 1000000000000000 i=65536: 10000000000000000 i=131072: 100000000000000000 i=262144: 1000000000000000000 i=524288: 10000000000000000000 i=1048576: 100000000000000000000 i=2097152: 1000000000000000000000 i=4194304: 10000000000000000000000 i=8388608: 100000000000000000000000 i=16777216: 1000000000000000000000000 i=33554432: 10000000000000000000000000 i=67108864: 100000000000000000000000000 i=134217728: 1000000000000000000000000000 i=268435456: 10000000000000000000000000000 i=536870912: 100000000000000000000000000000 i=1073741824: 1000000000000000000000000000000 i=-2147483648: 10000000000000000000000000000000 i=0: 02、右移(>>)
符号位不变,其他位移动
<span style="font-size:18px;">import java.util.*; public class Test{ public static void main(String[] args){ int k=-2147483648; System.out.println("k=" +k+":\t"+ Integer.toBinaryString(k)); for(int i= 1 ; i <= 32 ; i ++ ){ k >>= 1; System.out.println("k=" +k+":\t"+ Integer.toBinaryString(k)); } } }</span>
<span style="font-size:18px;">k=-2147483648: 10000000000000000000000000000000 k=-1073741824: 11000000000000000000000000000000 k=-536870912: 11100000000000000000000000000000 k=-268435456: 11110000000000000000000000000000 k=-134217728: 11111000000000000000000000000000 k=-67108864: 11111100000000000000000000000000 k=-33554432: 11111110000000000000000000000000 k=-16777216: 11111111000000000000000000000000 k=-8388608: 11111111100000000000000000000000 k=-4194304: 11111111110000000000000000000000 k=-2097152: 11111111111000000000000000000000 k=-1048576: 11111111111100000000000000000000 k=-524288: 11111111111110000000000000000000 k=-262144: 11111111111111000000000000000000 k=-131072: 11111111111111100000000000000000 k=-65536: 11111111111111110000000000000000 k=-32768: 11111111111111111000000000000000 k=-16384: 11111111111111111100000000000000 k=-8192: 11111111111111111110000000000000 k=-4096: 11111111111111111111000000000000 k=-2048: 11111111111111111111100000000000 k=-1024: 11111111111111111111110000000000 k=-512: 11111111111111111111111000000000 k=-256: 11111111111111111111111100000000 k=-128: 11111111111111111111111110000000 k=-64: 11111111111111111111111111000000 k=-32: 11111111111111111111111111100000 k=-16: 11111111111111111111111111110000 k=-8: 11111111111111111111111111111000 k=-4: 11111111111111111111111111111100 k=-2: 11111111111111111111111111111110 k=-1: 11111111111111111111111111111111 k=-1: 11111111111111111111111111111111</span>3、无符号右移(>>>)
高位由0补齐。
<span style="font-size:18px;">import java.util.*; public class Test{ public static void main(String[] args){ int k=0xFFFFFFFE; System.out.println("k=" +k+":\t"+ Integer.toBinaryString(k)); for(int i= 1 ; i <= 64 ; i ++ ){ k >>>= 1; System.out.println("k=" +k+":\t"+ Integer.toBinaryString(k)); } } }</span>
<span style="font-size:18px;">k=-2: 11111111111111111111111111111110 k=2147483647: 1111111111111111111111111111111 k=1073741823: 111111111111111111111111111111 k=536870911: 11111111111111111111111111111 k=268435455: 1111111111111111111111111111 k=134217727: 111111111111111111111111111 k=67108863: 11111111111111111111111111 k=33554431: 1111111111111111111111111 k=16777215: 111111111111111111111111 k=8388607: 11111111111111111111111 k=4194303: 1111111111111111111111 k=2097151: 111111111111111111111 k=1048575: 11111111111111111111 k=524287: 1111111111111111111 k=262143: 111111111111111111 k=131071: 11111111111111111 k=65535: 1111111111111111 k=32767: 111111111111111 k=16383: 11111111111111 k=8191: 1111111111111 k=4095: 111111111111 k=2047: 11111111111 k=1023: 1111111111 k=511: 111111111 k=255: 11111111 k=127: 1111111 k=63: 111111 k=31: 11111 k=15: 1111 k=7: 111 k=3: 11 k=1: 1 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 k=0: 0 </span>
谜题:
<span style="font-size:18px;">int i = 0; while(-1<<i != 0){ i++; } System.out.println(i);</span>
最后输出什么?实际上是陷入了死循环。
因为在这里-1不可能向左移动32位(左移或右移的最大位数是31或63),所以也就不可能得到0,因此也就不可能推出循环了。
如果想要退出循环,怎么办?
<span style="font-size:18px;">int count = 0; for(int i = -1 ;i != 0 ; i <<= 1){ count++; } System.out.println(count);</span>
如此可以打印出32;
原文地址:http://blog.csdn.net/havedream_one/article/details/45038603