直接看代碼以及註釋吧。
@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