java的移位运算和位运算

/**
 * @(#)Test.java
 *
 * Test application
 *
 * @author 
 * @version 1.00 2013/10/4
 */
 
public class Test {
    
    public static void main(String[] args) {
    	int a=1>>2;
    	int b=-1>>2;
    	int c=1<<2;
    	int d=-1<<2;
    	int e=3>>>2;
    	// a,b,c,d,e 的结果都是多少?
    	System.out.println("1>>2="+a);
    	System.out.println("-1>>2="+b);
    	System.out.println("12>>2="+(12>>2));
    	System.out.println("-12>>2="+(-12>>2));
    	System.out.println("-24>>2="+(-24>>2));
    	System.out.println("-24>>5="+(-24>>5));
    	System.out.println("-2>>2="+(-2>>2));
    	System.out.println("-2>>1="+(-2>>1));
    	System.out.println("-1>>1="+(-1>>1));
    	System.out.println("1<<2="+c);
    	System.out.println("-1<<2="+d);
    	System.out.println("3>>>2="+e);
    	System.out.println("~2="+~2);
    	System.out.println("2&3="+(2&3));
    	System.out.println("2|3="+(2|3));
    	System.out.println("~-5="+~-5);
    	System.out.println("13&7="+(13&7));
    	System.out.println("5|4="+(5|4));
    	System.out.println("-3^3="+(-3^3));
    	//无论是加减乘除还是位运算 移位运算 都是补码形式进行的
    }
}

--------------------Configuration: Test - JDK version 1.7.0_01 <Default> - <Default>--------------------
1>>2=0
-1>>2=-1
12>>2=3
-12>>2=-3
-24>>2=-6
-24>>5=-1
-2>>2=-1
-2>>1=-1
-1>>1=-1
1<<2=4
-1<<2=-4
3>>>2=0
~2=-3
2&3=2
2|3=3
~-5=4
13&7=5
5|4=5
-3^3=-2

Process completed.


左移就是乘2  注意不要溢出了 溢出之后没有固定值 按照移位规则进行   右移就是除以2   负数不够除了就是返回-1  正数不够除 了返回0  其实这也是移位规则决定的

 

你可能感兴趣的:(java)