使用BigInteger.setBit与BigInteger.testBit来实现权限控制

使用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得到新的角色编号。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值