位运算--权限运用
2018-03-27 15:53
260 查看
今天读了fastjson的源码,受益良多.那位运算来分享一下.
以前一直不明白位运算有什么用,现在明白位运算在做权限的时候用处很大.
1. 初始化权限int select=1;//0001
int insert=2;//0010
int update=4;//0100
int delete=8;//10002. 获取权限
如A用户仅有读写操作,(至于怎么知道他有读写操作权限,这里就不描述了),那么他的权限int值为int p=0;
p |= select;
P |= insert;3. 判断权限
在上1步已经获取了权限的数字p,则用下面的方法判断是否有权限; if((p&select)==select){
System.out.println("select");
}else {
System.out.println("can not select");
}
if ((p & insert) == insert) {
System.out.println("insert");
} else {
System.out.println("can not insert");
}
if((p&update)==update){
System.out.println("update");
}else{
System.out.println("can not update");
}
if ((p & delete) == delete) {
System.out.println("delete");
} else {
System.out.println("can not delete");
}4. 小结
这里select,insert,update,delete 只是示例,应用的时候可以将他们设置为枚举,利用枚举的ordinal()方法,
再用1<<ordinal()确定权限的数值,
以前一直不明白位运算有什么用,现在明白位运算在做权限的时候用处很大.
1. 初始化权限int select=1;//0001
int insert=2;//0010
int update=4;//0100
int delete=8;//10002. 获取权限
如A用户仅有读写操作,(至于怎么知道他有读写操作权限,这里就不描述了),那么他的权限int值为int p=0;
p |= select;
P |= insert;3. 判断权限
在上1步已经获取了权限的数字p,则用下面的方法判断是否有权限; if((p&select)==select){
System.out.println("select");
}else {
System.out.println("can not select");
}
if ((p & insert) == insert) {
System.out.println("insert");
} else {
System.out.println("can not insert");
}
if((p&update)==update){
System.out.println("update");
}else{
System.out.println("can not update");
}
if ((p & delete) == delete) {
System.out.println("delete");
} else {
System.out.println("can not delete");
}4. 小结
这里select,insert,update,delete 只是示例,应用的时候可以将他们设置为枚举,利用枚举的ordinal()方法,
再用1<<ordinal()确定权限的数值,
相关文章推荐
- (转)C#运用位运算进行权限管理
- 运用PHP位运算做网站权限
- .net 运用二进制位运算进行权限管理
- C语言位运算运用二:输出二进制序列中所有的偶数位和奇数位
- Oracle 中运用rollup和cube实现汇总运算
- C#枚举中的位运算权限分配浅谈
- 异或运算的神奇运用
- C#枚举中的位运算权限分配浅谈
- IHttpHandlerFactory的运用(权限控制)
- 位与运算在权限管理中的应用
- 关于灵活运用membership,role进行权限管理
- (转)巧妙运用C语言位运算
- C#中二进制运算在权限验证的应用
- 程序中位运算的运用
- 基于逻辑运算的简单权限系统(实现) JS 版
- 基于逻辑运算的简单权限系统(实现) JS 版
- 简易运算(运用策略模式实现)
- 位运算基本概念及简单运用
- C# 位运算权限管理
- jsp运用小例子 乘法口诀运算代码