java二进制权限_Java 二进制权限控制算法

二进制权限控制算法,主要是利用了(&,|, ~)运算的特性。

String s1 = "0000000000000000000000000000000000000001"; // 添加

String s2 = "0000000000000000000000000000000000000010"; // 删除

String s3 = "0000000000000000000000000000000000000100"; // 修改

String s4 = "0000000000000000000000000000000000001000"; // 查看

long iPermission1 = Long.parseLong(s1, 2) | Long.parseLong(s2, 2); // 添加权限

long iPermission2 = iPermission1 & (~Long.parseLong(s1, 2)); // 删除权限

long is1 = Long.parseLong(s1, 2);

long is2 = Long.parseLong(s2, 2);

long is3 = Long.parseLong(s3, 2);

long is4 = Long.parseLong(s4, 2);

/* iPermission1 拥有的权限 */

if ((iPermission1 & is1) == is1) {

System.out.println("iPermission1有添加");

}

if ((iPermission1 & is2) == is2) {

System.out.println("iPermission1有删除");

}

/* iPermission2 拥有的权限 */

if ((iPermission2 & is1) == is1) {

System.out.println("iPermission2有添加");

}

if ((iPermission2 & is2) == is2) {

System.out.println("iPermission2有删除");

}