# 咖啡伴侣

posts - 163, comments - 156, trackbacks - 0, articles - 2

/*
* show the arthimetic character of '<<' '>>' '>>>'
*/

public class TestArithmetic {
public TestArithmetic() {
}

public   static void   main(String [] args){
int minus = -10;
System.out.println(" Binary of -10 is " + Integer.toBinaryString(minus));
System.out.println(" Arthimetic minus by -10 << 2 = " + (minus<<2) + " Binary is " + Integer.toBinaryString(minus<<2));
System.out.println(" Arthimetic minus by -10 >> 2 = " + (minus>>2) + " Binary is " + Integer.toBinaryString(minus>>2));
System.out.println(" Arthimetic minus by -10 >>>2 =   " + (minus >>> 2) + " Binary is " + Integer.toBinaryString(minus>>>2)
+ ",length is " + Integer.toBinaryString(minus>>>2).length());

int plus = 10;
System.out.println(" Binary of 10 is " + Integer.toBinaryString(plus));
System.out.println(" Arthimetic minus by 10 << 2 = " + (plus<<2)+ "Binary is " + Integer.toBinaryString(plus<<2));
System.out.println(" Arthimetic minus by 10 >> 2 = " + (plus>>2)+ "Binary is "+ Integer.toBinaryString(plus>>2));
System.out.println(" Arthimetic minus by 10 >>>2 =   " + (plus >>> 2)+ "Binary is "+ Integer.toBinaryString(plus >>> 2));
}

（1）正数的补码：与原码相同。

（2）负数的补码：符号位为1，其余位为该数绝对值的原码按位取反；然后整个数加1。

（1）如果补码的符号位为“0”，表示是一个正数，所以补码就是该数的原码。
（2）如果补码的符号位为“1”，表示是一个负数，求原码的操作可以是：符号位为1，其余各位取反，然后再整个数加1。

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: