个人博客
位运算的简单应用-权限管理
位运算在实际的开发中,有很多巧妙的应用场景。如:
-
一个存放正整数的数组,里面有一个数字只出现一次,其它数字都出现两次,求只出现一次的数字。
-
不用临时变量,交换两个数字的值。
-
Android中Window设置Flag
public void setFlags(int flags, int mask) {
final WindowManager.LayoutParams attrs = getAttributes();
attrs.flags = (attrs.flags&~mask) | (flags&mask);
mForcedWindowFlags |= mask;
dispatchWindowAttributesChanged(attrs);
}
- 产品具有某些Feature,这些Feature可以利用位运算来存储。
下面利用位运算来简单模拟权限管理
权限管理类
public class PermissionManager {
public static final int PERMISSION_NONE = 1;
public static final int PERMISSION_A = 1 << 1;
public static final int PERMISSION_B = 1 << 2;
public static final int PERMISSION_C = 1 << 3;
public static final int PERMISSION_D = 1 << 4;
private int mPermission = PERMISSION_NONE;
private static class PermissionManagerHolder {
static PermissionManager sPermissionManager = new PermissionManager();
}
private PermissionManager() {
}
public static PermissionManager getInstance() {
return PermissionManagerHolder.sPermissionManager;
}
/**
* 添加权限
* @param permission
*/
public void addPermission(int permission) {
if (hasPermission(permission)) {
return;
}
mPermission |= permission;
}
/**
* 移除权限
* @param permission
*/
public void removePermission(int permission) {
if (!hasPermission(permission)) {
return;
}
mPermission &= ~permission;
}
/**
* 判断是否有权限
* @param permission
* @return
*/
public boolean hasPermission(int permission) {
return (mPermission & permission) != 0;
}
}
使用
public static void main(String[] args) {
System.out.println("没有添加任何权限----------");
PermissionManager permissionManager = PermissionManager.getInstance();
checkPermission(permissionManager);
System.out.println("添加权限A----------");
System.out.println("添加权限B----------");
System.out.println("添加权限C----------");
permissionManager.addPermission(PermissionManager.PERMISSION_A);
permissionManager.addPermission(PermissionManager.PERMISSION_B);
permissionManager.addPermission(PermissionManager.PERMISSION_C);
checkPermission(permissionManager);
System.out.println("删除权限C----------");
permissionManager.removePermission(PermissionManager.PERMISSION_C);
checkPermission(permissionManager);
}
public static void checkPermission(PermissionManager permissionManager) {
System.out.println("PERMISSION_A exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_A));
System.out.println("PERMISSION_B exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_B));
System.out.println("PERMISSION_C exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_C));
System.out.println("PERMISSION_D exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_D));
}
输出结果:
没有添加任何权限----------
PERMISSION_A exist:false
PERMISSION_B exist:false
PERMISSION_C exist:false
PERMISSION_D exist:false
添加权限A----------
添加权限B----------
添加权限C----------
PERMISSION_A exist:true
PERMISSION_B exist:true
PERMISSION_C exist:true
PERMISSION_D exist:false
删除权限C----------
PERMISSION_A exist:true
PERMISSION_B exist:true
PERMISSION_C exist:false
PERMISSION_D exist:false
位运算并不限于权限管理,具有多个状态的应用场景,都可以用到。