个人博客

http://www.milovetingting.cn

位运算的简单应用-权限管理

位运算在实际的开发中,有很多巧妙的应用场景。如:

  1. 一个存放正整数的数组,里面有一个数字只出现一次,其它数字都出现两次,求只出现一次的数字。

  2. 不用临时变量,交换两个数字的值。

  3. Android中Window设置Flag

1
2
3
4
5
6
public void setFlags(int flags, int mask) {
final WindowManager.LayoutParams attrs = getAttributes();
attrs.flags = (attrs.flags&~mask) | (flags&mask);
mForcedWindowFlags |= mask;
dispatchWindowAttributesChanged(attrs);
}
  1. 产品具有某些Feature,这些Feature可以利用位运算来存储。

下面利用位运算来简单模拟权限管理

权限管理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
}

}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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));
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
没有添加任何权限----------
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

位运算并不限于权限管理,具有多个状态的应用场景,都可以用到。