java位运算示例

左移、右移、无符号右移

左移

int型在java中占4个字节共32位
测试代码

public class test {    
    public static void main(String[] args) {
        System.out.println("---------左移--------");
        //-----------正数左移-------------
        //00000000 00000000 00000000 00000100
        //左移1位
        //00000000 00000000 00000000 00001000 = 8
        System.out.println("4<<1="+(4<<1));
        //00000000 00000000 00000000 00000100
        //左移2位
        //00000000 00000000 00000000 00010000 = 16
        System.out.println("4<<2="+(4<<2));

        //最大值为Integer.MAX_VALUE就是0x7FFFFFFF
        //01111111 11111111 11111111 11111111
        //11111111 11111111 11111111 11111110
        //计算机存储为补码,高位1为符号位,其他取反加1
        //10000000000000000000000000000000001+1
        //10000000000000000000000000000000010 = -2
        System.out.println("0x7FFFFFFF<<1="+(0x7FFFFFFF<<1));

        //-----------负数左移-------------
        System.out.println("-1<<1="+(-1<<1));
        System.out.println("-2<<1="+(-2<<1));       
    }
}

测试结果

---------左移--------
4<<1=8
4<<2=16
0x7FFFFFFF<<1=-2
-1<<1=-2
-2<<1=-4

右移

测试代码

public class test {    
    public static void main(String[] args) {
        System.out.println("---------右移--------");
        //--------正数右移--------
        //符号位不变,从下一位右移补0
        //00000000 00000000 00000000 00000100
        //右移1位
        //00000000 00000000 00000000 00000010 = 2
        System.out.println("4>>1="+(4>>1));
        //00000000 00000000 00000000 00000100
        //右移2位
        //00000000 00000000 00000000 00000001 = 1
        System.out.println("4>>2="+(4>>2));
        //00000000 00000000 00000000 00000000 高位补0
        System.out.println("0>>1="+(0>>1));

         //--------负数右移--------
        //10000000 00000000 00000000 00000010
        //10000000 00000000 00000000 00000001
        System.out.println("-2>>1="+(-2>>1));
        System.out.println("-4>>1="+(-4>>1));
    }
}

测试结果

---------右移--------
4>>1=2
4>>2=1
0>>1=0
-2>>1=-1
-4>>1=-2

无符号右移

测试代码

 public class test {    
    public static void main(String[] args) {
        System.out.println("---------无符号右移--------");
        //-----------正数右移----------
        //00000000 00000000 00000000 00000100
        //右移1位
        //00000000 00000000 00000000 00000010 = 2
        System.out.println("4>>>1="+(4>>>1));


        //11111111 11111111 11111111 11111111
        //01111111 11111111 11111111 11111111 = 最大值0x7fffffff
        System.out.println("0x7fffffff="+0x7fffffff);
        System.out.println("0xffffffff>>>1="+(0xffffffff>>>1));
        //-----------负数右移----------
        //10000000 00000000 00000000 00000010
        //取反加1
        //11111111 11111111 11111111 11111110
        //右移一位
        //011111111 11111111 11111111 1111111
        System.out.println("-2>>>1="+(-2>>>1));

        //10000000 00000000 00000000 00000100
        //取反加1
        //11111111 11111111 11111111 11111100
        //011111111 11111111 11111111 1111110
        System.out.println("-4>>>1="+(-4>>>1));  
    }
 }

测试结果

---------无符号右移--------
4>>>1=2
0x7fffffff=2147483647
0xffffffff>>>1=2147483647
-2>>>1=2147483647
-4>>>1=2147483646

与、或、非、亦或

测试代码

public class test {    
    public static void main(String[] args) {
        //8->00000000 00000000 00000000 00001000
        //1->00000000 00000000 00000000 00000001
        // ->00000000 00000000 00000000 00000000   
        System.out.println(8&1);        
        //20->00000000 00000000 00000000 00010100
        //8 ->00000000 00000000 00000000 00001000
        //  ->00000000 00000000 00000000 00000000  
        System.out.println(20&8);

        //8->00000000 00000000 00000000 00001000
        //1->00000000 00000000 00000000 00000001
        // ->00000000 00000000 00000000 00001001 = 9
        System.out.println(8|1);
        //20->00000000 00000000 00000000 00010100
        //8 ->00000000 00000000 00000000 00001000
        //  ->00000000 00000000 00000000 00011100 = 28
        System.out.println(20|8);

        //0 ->00000000 00000000 00000000 00000000
        //取反 11111111 11111111 11111111 11111111
        //补码 10000000 00000000 00000000 00000001 = -1//符号位不动,取反加1
        System.out.println(~0);
        System.out.println(~1);

        //20->00000000 00000000 00000000 00000101
        //8 ->00000000 00000000 00000000 00000011
        //  ->00000000 00000000 00000000 00000110 = 6
        System.out.println(5^3);
    }    
}

测试结果

0
0
9
28
-1
-2
6

&运算

8&1
8 -> 1000
1 -> 0001
------------
     0000

20&8
20->10100
8 ->01000
--------------
    00000

|运算

8|1
8 -> 1000
1 -> 0001
------------
     1001

20|8
20->10100
8 ->01000
--------------
    11100

~运算

0 -> 0000 0000
取反 1111 1111
     //因为计算机存储存的是补码,去反后最高位是1所以被按作负数求补即符号位不变 其他位求反再加1
     1000 0000 +1
     1000 0001
----------------
     -1  
~1
1 -> 0000 0001
取反  1111 1110
     1000 0001 +1 
     1000 0010
-----------------
    -2     

^异或

5^3
5 -> 0101
3 -> 0011
------------
     0110 (6)   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值