
目录
在Think in Java中有这么一段话“对char,byte或者short进行移位处理,那么在移位进行之前,它们会自动转换成一个int。只有右侧的5个低位才会有用。这样可防止我们在一个int数里移动不切实际的位数。若对一个long值进行处理,最后得到的结果也是long。此时只会用到右侧的6个低位,防止移动超过long值里现成的位数。”
对上面那段话的理解是:移位操作符操作的运算对象是二进制的“位”,int类型是32位也就是2的5次幂 !如果移32位以上,那么原来的数的信息会全部丢失,这样也就没有什么意义了!所以上面的“只有右侧的5个低位才会有用”说的是:移位操作符右端的那个数(化成二进制)的低5位才有用,即
X < <y;
是指y的低5位才有用,即不能大于32。 而对于long型也是同样的道理!
package com.xingle_test.operator; /**
*
* @ClassName: shifting 移位操作
* @author Xingle
* @date 2014-7-22 上午9:30:16
*/
public class shifting { public static void main(String[] args) {
int i = 1;
pBinInt("" + i + "", i);
int i_1 = i << 2;
pBinInt("<<2", i_1);
int i_2 = i << 34;
pBinInt("<<34", i_2);
System.out.println(); int k = -1;
pBinInt("" + k + "", k);
int k_1 = k << 2;
pBinInt("<<2", k_1);
int k_2 = k << 34;
pBinInt("<<34", k_2);
System.out.println(); long j = -12;
pBinLong("" + j + "", j);
long j_1 = j >> 2;
pBinLong(">>2", j_1);
long j_2 = j >> 66;
pBinLong(">>66", j_2);
System.out.println(); } /**
* long 打印二进制
*
* @param s
* @param j
* @author xingle
* @data 2014-7-22 上午9:41:17
*/
private static void pBinLong(String s, long l) {
System.out.println(s + ", long: " + l + ", binary: ");
System.out.print(" ");
for (int i = 63; i >= 0; i--)
if (((1L << i) & l) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
} /**
* int 打印二进制
*
* @param s
* @param i
* @author xingle
* @data 2014-7-22 上午9:31:42
*/
private static void pBinInt(String s, int i) {
System.out.println(s + ", int: " + i + ", binary: ");
System.out.print(" ");
for (int j = 31; j >= 0; j--)
if (((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
} }
执行结果:
1, int: 1, binary:
00000000000000000000000000000001
<<2, int: 4, binary:
00000000000000000000000000000100
<<34, int: 4, binary:
00000000000000000000000000000100
-1, int: -1, binary:
11111111111111111111111111111111
<<2, int: -4, binary:
11111111111111111111111111111100
<<34, int: -4, binary:
11111111111111111111111111111100
-12, long: -12, binary:
1111111111111111111111111111111111111111111111111111111111110100
>>2, long: -3, binary:
1111111111111111111111111111111111111111111111111111111111111101
>>66, long: -3, binary:
1111111111111111111111111111111111111111111111111111111111111101
从以上结可以看到对于32位的int型,左移2位和左移34位的结果是相同的;
对于long型,左移2位和左移66位的结果是一样的。
在java中无论左移右移,会遵循下面的规则
value<<n(其中value为int,n>=0) 等价于 value<<(n%32)
value>>n (其中value为int,n>=0) 等价于 value>>(n%32)
value>>>n (其中value为int,n>=0) 等价于 value>>>(n%32)
对于long类型的:
value<<n(其中value为long,n>=0) 等价于 value<<(n%64)
value>>n (其中value为long,n>=0) 等价于 value>>(n%64)
value>>>n (其中value为long,n>=0) 等价于 value>>>(n%64)
/**
* @Title: shifting.java
* @Description: TODO
* @author :Xingle
* @date 2014-7-22 上午9:30:16
* @version
*/ package com.xingle_test.operator; /**
*
* @ClassName: shifting 移位操作
* @author Xingle
* @date 2014-7-22 上午9:30:16
*/
public class shifting { public static void main(String[] args) { int i = 100;
pBinInt("" + i + "", i);
int i_1 = i << -38;
pBinInt("<<-38", i_1);
int i_2 = i << 26;
pBinInt("<<26", i_2);
System.out.println(); int k = -100;
pBinInt("" + k + "", k);
int k_1 = k << -38;
pBinInt("<<-38", k_1);
int k_2 = k << 26;
pBinInt("<<26", k_2);
System.out.println(); long j = 12;
pBinLong("" + j + "", j);
long j_1 = j << -3;
pBinLong("<<-3", j_1);
long j_2 = j << 61;
pBinLong("<<61", j_2);
System.out.println(); long a = -112;
pBinLong("" + a + "", a);
long a_1 = a >>> -67;
pBinLong(">>> -67", a_1);
long a_2 = a >>> 61;
pBinLong(">>> 61", a_2);
System.out.println(); } /**
* long 打印二进制
*
* @param s
* @param j
* @author xingle
* @data 2014-7-22 上午9:41:17
*/
private static void pBinLong(String s, long l) {
System.out.println(s + ", long: " + l + ", binary: ");
System.out.print(" ");
for (int i = 63; i >= 0; i--)
if (((1L << i) & l) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
} /**
* int 打印二进制
*
* @param s
* @param i
* @author xingle
* @data 2014-7-22 上午9:31:42
*/
private static void pBinInt(String s, int i) {
System.out.println(s + ", int: " + i + ", binary: ");
System.out.print(" ");
for (int j = 31; j >= 0; j--)
if (((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
} }
执行结果:
100, int: 100, binary:
00000000000000000000000001100100
<<-38, int: -1879048192, binary:
10010000000000000000000000000000
<<26, int: -1879048192, binary:
10010000000000000000000000000000
-100, int: -100, binary:
11111111111111111111111110011100
<<-38, int: 1879048192, binary:
01110000000000000000000000000000
<<26, int: 1879048192, binary:
01110000000000000000000000000000
12, long: 12, binary:
0000000000000000000000000000000000000000000000000000000000001100
<<-3, long: -9223372036854775808, binary:
1000000000000000000000000000000000000000000000000000000000000000
<<61, long: -9223372036854775808, binary:
1000000000000000000000000000000000000000000000000000000000000000
-112, long: -112, binary:
1111111111111111111111111111111111111111111111111111111110010000
>>> -67, long: 7, binary:
0000000000000000000000000000000000000000000000000000000000000111
>>> 61, long: 7, binary:
0000000000000000000000000000000000000000000000000000000000000111
由以上可以看出:
对于int,左移-38和左移26是相等的;
对于long,左移-3和左移61是相等的;
对于无符号右移,右移-67和右移61是相等的;
结论:
value << -n(value为int,n>=0) 等价于 value << (-n & 31)
value >> -n(value为int,n>=0) 等价于 value >> (-n & 31)
value >>> -n(value为int,n>=0) 等价于 value >>> (-n & 31)
而对于long
value << -n(value为long,n>=0) 等价于 value << (-n & 63)
value >> -n(value为long,n>=0) 等价于 value >> (-n & 63)
value >>> -n(value为long,n>=0) 等价于 value >>> (-n & 63)