原
使用BigInteger.setBit与BigInteger.testBit来实现权限控制
<div class="article-info-box">
<div class="article-bar-top d-flex">
<span class="time">2017年04月09日 01:03:12</span>
<div class="float-right">
<span class="read-count">阅读数:1592</span>
</div>
</div>
</div>
<article>
<div id="article_content" class="article_content csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/htmledit_views-0a60691e80.css">
<div class="htmledit_views">
今天看到一个项目使用了BigInteger.setBit与BigInteger.testBit来实现权限控制,觉得很有趣,mark一下
首先,列出一下这两个方法的解释:
1.testBit方法的解释:
boolean java.math.BigInteger.testBit(int n)
Returns true
if and only if the designated bit is set. (Computes ((this & (1<<n)) != 0)
.)
-
Parameters:
- n index of bit to test. Returns:
-
true
if and only if the designated bit is set.
Throws:
ArithmeticException - n
is negative.
意思就是将1左移n位,与this做&运算,其实就是判断当前数(要写成二进制)第n+1位上的数是不是为1,是的话返回true
2.setBit方法的解释
BigInteger java.math.BigInteger.setBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. (Computes (this | (1<<n))
.)
-
Parameters:
- n index of bit to set. Returns:
-
this | (1<<n)
Throws:
ArithmeticException -
n
is negative.
意思就是将1左移n位,与this对象做|运算,这样的话,就会将this的二进制格式的第n+1位的数变为1.这样很明显就和上一个方法形成一对,
n可以作为某个功能编号,而角色可以使用setBit的方法设置编号,然后使用testBit来测试是不是含有n编号的功能。
如果每次有添加多个新的功能,那么就用这些功能编号依次给原来的角色编号执行setBit得到新的角色编号。