java编程中使用二进制进行权限或状态控制

直接看代码以及注释吧。

    @Test
    public void main() {
        // PC WEB端
        int pc = 1 << 0;// ...0001=1

        // Android端
        int android = 1 << 1;// ...0010=2

        // iOS端
        int ios = 1 << 2;// ...0100=4

        // WindowsPhone
        int wp = 1 << 3;// ...1000=8

        //----------------校验开始-----------------
        //表示只适用于PC WEB端
        int pcAndAndroid = pc | android;
        //判断是否有android端
        System.out.println((pcAndAndroid & android) == android);// true
        //判断是否有ios
        System.out.println((pcAndAndroid & ios) == ios);// false
        //去掉android,加入ios,判断是否有ios,   pcAndAndroid & (~android) 去掉android
        System.out.println((((pcAndAndroid & (~android)) | ios) & ios) == ios);// true
        //去掉android,加入ios,判断是否有android,  false
        System.out.println((((pcAndAndroid & (~android)) | ios) & android) == android);// false

        //--------------
        // android/ios/win phone
        int aiw = android | ios | wp;
        //判断是否有android 并且有win phone
        System.out.println((aiw & (android | wp)) == (android | wp));// true
        //判断是否有android 并且有pc
        System.out.println((aiw & (android | pc)) == (android | pc));// false
    }

顺便看看Oracle数据库怎么弄,比较麻烦咯,不建议这么搞,毕竟数据存进去也得计算一次

位与的操作,在应用程序里是经常会用到的, 
Oracle也提供这样的函数 
BITAND(x,y) 
SQL> select bitand(7, 31) from dual; 
BITAND(7,31) 
------------ 
7 
但是Oracle里没有提供bitOr的函数,没关系 
bitand和bitOR是有关系的。 
关系如下 
BITOR(x,y) = (x + y) - BITAND(x, y); 
BITXOR(x,y) = BITOR(x,y) - BITAND(x,y) = (x + y) - BITAND(x, y) * 2; 
SQL> select 7+31-bitand(7, 31) as bitor from dual; 
BITOR 
---------- 
31 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值