java中的位运算


public class weiyunsuan {
    
public static void main(String[] args) {
        
int a=-6;//          1111 1111 1111 1111 1111 1111 1111 1010
        int b=3;//           0000 0000 0000 0000 0000 0000 0000 0011
        int c=6//          0000 0000 0000 0000 0000 0000 0000 0110
        zuoyi(a, b);//-48    1111 1111 1111 1111 1111 1111 1101 0000
        youyi(a, b);//-1     1111 1111 1111 1111 1111 1111 1111 1111
        wufuhaoyouyi(a, b);//0001 1111 1111 1111 1111 1111 1111 1111
        zuoyi(c, b);//48     0000 0000 0000 0000 0000 0000 0011 0000
        youyi(c, b);//0      0000 0000 0000 0000 0000 0000 0000 0000
        wufuhaoyouyi(c, b);//0000 0000 0000 0000 0000 0000 0000 0000
        yu(a, b);//2         0000 0000 0000 0000 0000 0000 0000 0010
        huo(a, b);//-5       1111 1111 1111 1111 1111 1111 1111 1011
        fei(a);//5           0000 0000 0000 0000 0000 0000 0000 0101
        yihuo(a, b);//-7     1111 1111 1111 1111 1111 1111 1111 1001
    }
    
public static void zuoyi(int a,int b){
        System.out.println(a
<<b);
    }
    
public static void youyi(int a,int b){
        System.out.println(a
>>b);
    }
    
public static void wufuhaoyouyi(int a,int b){
        System.out.println(a
>>>b);
    }
    
public static void yu(int a,int b){
        System.out.println(a
&b);
    }
    
public static void huo(int a,int b){
        System.out.println(a
|b);
    }
    
public static void fei(int a){
        System.out.println(
~a);
    }
    
public static void yihuo(int a,int b){
        System.out.println(a
^b);
    }
}

原文地址:https://www.cnblogs.com/zhonghan/p/1442320.html