# 用标志位表示状态

\ OR
& AND
~ NOT
^ XOR 异或
>> - 右移
<< - 左移
>>> - 无符号右移(最高位始终补0)

1 | 0   // 1, 两个有一个 1 结果为 1, 其他情况都为 0
1 & 0   // 0 两个都为 1 结果为 1, 其他情况都为 0
~ 1     // 0 单目运算, 取运算值的反值
1 ^ 0   // 1 参与运算的两个值不同, 则为 1, 相同则为 0
1 << 1  // 10 将左边的值 左移 指定位数
1010 >> 1   // 101 将左边值 右移 指定位数

##### 标志位: 将一个整数的某一位作为标志位, 并赋予特定的含义

public static int NONE      = 0;
public static int ADD       = 1;
public static int DELETE    = 2;
public static int READ      = 4;
public static int UPDATE    = 8;


1   =   0001
2   =   0010
4   =   0100
8   =   1000


permission |= ADD | READ;
//  0 | 0001 | 0100 = 0101
/**
0000
0001
0100
----
0101
*/


boolean canRead = (permission & READ) != 0
// 0101 & 0100 = 0100 = ADD
/**
0101
0100
----
0100
*/


permission ^= READ
// 0101 ^ 0100 =  0001
/**
0101
0100
----
0001
*/


permission &= ~ READ
// 0101 & 1011 = 0001
/**
~0100
----
1011
&0101
----
0001
*/


#### 标志位的优势

setCanRead(true);
setCanUpdate(false);
setCanDelete(false);


addPermission(READ | ADD);


isCanRead();


hasPermission(ADD | READ);


isCanAdd() isCanRead && !isCanUpdate() && !isCanDelete()


isJustAllow(READ | ADD);


class Permission {
public static final int NONE        = 0;
public static final int ADD         = 1;
public static final int DELETE      = 2;
public static final int READ        = 4;
public static final int UPDATE      = 8;

private int mPermission = NONE;

mPermission |= permission;
}
public boolean hasPermission(int permission){
return  (mPermission & permission) != NONE;
}
public boolean isJustAllow(int permission){
return mPermission == permission;
}
public void removePermission(int permission){
mPermission ^= permission;
}
}