BigInteger权限判断

本文介绍如何利用BigInteger的setBit与testBit方法实现权限系统的设置与判断。通过具体示例代码展示如何为不同用户分配特定权限,并验证这些权限。


1、上篇文章已经解析了BigInteger.testBit(int n)与setBit(int n)

http://blog.youkuaiyun.com/u014520797/article/details/64122211


2、在进行开发权限系统时可以使用BigInteger.setBit(int n)与testBit(int n)进行权限设置和权限判断


3、代码

public static void main(String[] args) {
		BigInteger bi = new BigInteger("0"); 
		bi =bi.setBit(1);
		bi =bi.setBit(2);
		bi =bi.setBit(3);
		bi =bi.setBit(6);
		
		Boolean b1, b2,b3,b5;
		b1 = bi.testBit(1);
		b2 = bi.testBit(2);
		b3 = bi.testBit(3);
		b5 = bi.testBit(7);
		
		String str1 = "Test Bit on " + bi + " at index 1 returns " +b1;
		String str2 = "Test Bit on " + bi + " at index 2 returns " +b2;
		String str3 = "Test Bit on " + bi + " at index 3 returns " +b3;
		String str5 = "Test Bit on " + bi + " at index 7 returns " +b5;
		
		System.out.println( str1 );
		System.out.println( str2 );
		System.out.println( str3 );
		System.out.println( str5 );
	}

结果

Test Bit on 78 at index 1 returns true
Test Bit on 78 at index 2 returns true
Test Bit on 78 at index 3 returns true
Test Bit on 78 at index 7 returns false


4、解释

//在权限系统中,当一个权限资源的id为123456789.....
//当A拥有23资源的权限,那么就可以bi =bi.setBit(2或3或其他权限id),然后再去用bi.testBit(2或3或其他权限id)去验证,
//当拥有该权限时,便会返回true
//当B拥有456资源的权限,...........
//当C拥有789资源的权限,...........
			
//当然  也可不需要一个的去设置bi =bi.setBit(int n),比如A拥有2,3权限时,那么他的权限值为12(即1100),
//所以可以用new BigInteger("12").setBit(2或3或其他权限id)


5、如果有不懂,可以看上一篇的方法解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值