public class Rights {
public static void main(String[] args) {
int a= 1<<0; // 001 状态a
int b= 1<<1; // 010 状态b
int c= 1<<2; // 100 状态c
int ab = include(a, b) ;// 001 | 010 = 011 初始状态
System.out.println(include(ab, c)); // 011 | 100 = 111 添加c的状态
System.out.println(exclude(ab, b)); // 011 & (~010) = 011 & 101 = 001 去除b的状态
System.out.println(access(ab, b)); // 011 & 010 = 010 判断是否有b的权限:(ab & b)==b
System.out.println(access(ab, c)); // 011 & 100 = 000
}
/**
* target权限添加到source
*/
public static int include(int source,int target){
return source|target;
}
/**
* 从source 排除target的权限
*/
public static int exclude(int source,int target){
return source&(~target);
}
/**
* source 是否包含target的权限
*/
public static boolean access(int source,int target){
return (source & target) == target ;
}
}
转载于:https://my.oschina.net/haoran100/blog/810032