java的三种移位方式 << ,>>, >>>。
>>表示右移,最高位由原来的符号为填充, <<表示为左移,则无所谓符号为,会将符号位侵占,同时最后1位补0; >>>表示右移,但是最高位不由原符号位填充。
同时如果是一个byte , char, short,移位的话,则会先提升成int型之后再移位。int,long都不用再提升。
byte a = (
byte)
0x40;
byte b = ( byte) (a << 1);
System. out.println(b);
System. out.println(a << 1);
byte c = ( byte) 0x80;
byte b = ( byte) (a << 1);
System. out.println(b);
System. out.println(a << 1);
byte c = ( byte) 0x80;
System.out.println(c >> 1);
输出为:
-128
128
-64
持续地左移移位会使数字一直乘2,直到数值越界。
public class a {
public static void main(String[] args) {
int a = 1;
int b = - 1;
test(a);
test(b);
test( 10);
test( 2);
}
public static void test( int i){
System. out.println( "初始值为: " + i);
for ( int a = 0; a != 31; a++){
System. out.print(i = (i << 1));
System. out.print( " ");
}
System. out.println();
}
public static void main(String[] args) {
int a = 1;
int b = - 1;
test(a);
test(b);
test( 10);
test( 2);
}
public static void test( int i){
System. out.println( "初始值为: " + i);
for ( int a = 0; a != 31; a++){
System. out.print(i = (i << 1));
System. out.print( " ");
}
System. out.println();
}
}
初始值为: 1
2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648
初始值为: -1
-2 -4 -8 -16 -32 -64 -128 -256 -512 -1024 -2048 -4096 -8192 -16384 -32768 -65536 -131072 -262144 -524288 -1048576 -2097152 -4194304 -8388608 -16777216 -33554432 -67108864 -134217728 -268435456 -536870912 -1073741824 -2147483648
初始值为: 10
20 40 80 160 320 640 1280 2560 5120 10240 20480 40960 81920 163840 327680 655360 1310720 2621440 5242880 10485760 20971520 41943040 83886080 167772160 335544320 671088640 1342177280 -1610612736 1073741824 -2147483648 0
初始值为: 2
4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648 0
持续地右移移位会使数字一直整除2,但是对-1不管用。
public class a {
public static void main(String[] args) {
int a = 1;
int b = - 1;
test(a);
test(b);
test( 10);
test( 2);
test( 16);
test(- 16);
}
public static void test( int i){
System. out.println( "初始值为: " + i);
for ( int a = 0; a != 31; a++){
System. out.print(i = (i >> 1));
System. out.print( " ");
}
System. out.println();
}
public static void main(String[] args) {
int a = 1;
int b = - 1;
test(a);
test(b);
test( 10);
test( 2);
test( 16);
test(- 16);
}
public static void test( int i){
System. out.println( "初始值为: " + i);
for ( int a = 0; a != 31; a++){
System. out.print(i = (i >> 1));
System. out.print( " ");
}
System. out.println();
}
}
初始值为: 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
初始值为: -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
初始值为: 10
5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
初始值为: 2
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
初始值为: 16
8 4 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
初始值为: -16
-8 -4 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1