请自行补习位运算相关知识
位运算
// 按位与 & ,相同位均为1则1, 否则为0
0b1100 & 0b1001 // 0b1000
// 按位或 | ,相同位只要有一个为1即为1
0b1100 | 0b1001 // 0b1101
// 按位异或 ^ ,相同位不同则为1, 否则为0
0b1100 ^ 0b1001 // 0b0101
// 按位取反 ~
~0b1100 // -0b1101
// 左移 <<
1 << 10 // 1024
// 带符号右移 >>
5 >> 1 // 2
-4 >> 1 // -2
// 无符号右移 >>> (部分语言支持)
-4 >>> 1 // 2147483646
位运算示例
// y 为下标, 从0开始
// 将 x 的指定位 y 置1
x |= (1<<y)
// 将 x 的指定位 y 置0
x &= ~(1<<y)
// 将 x 的指定位 y 取反
x ^= (1<<y)
// 取 x 的指定位 y 的值
x >> y&1
权限控制
// 可以复制到浏览器控制台测试输出结果
const P_0 = 0;
const P_1 = 1 << 0;
const P_2 = 1 << 1;
const P_3 = 1 << 2;
const P_4 = 1 << 3;
const P_5 = 1 << 4;
const P_6 = 1 << 5;
const P_7 = 1 << 6;
console.log('权限表:');
console.log('权限1: P_0 :', P_0.toString(2), P_0);
console.log('权限2: P_1 :', P_1.toString(2), P_1);
console.log('权限3: P_2 :', P_2.toString(2), P_2);
console.log('权限4: P_3 :', P_3.toString(2), P_3);
console.log('权限5: P_4 :', P_4.toString(2), P_4);
console.log('权限6: P_5 :', P_5.toString(2), P_5);
console.log('权限7: P_6 :', P_6.toString(2), P_6);
console.log('权限8: P_7 :', P_7.toString(2), P_7);
console.log('\n赋值对应权限:');
let pAllowed = P_0;
console.log('pAllowed 初始值:', pAllowed.toString(2));
console.log('pAllowed |= P_1 :', (pAllowed |= P_1).toString(2), pAllowed);
console.log('pAllowed |= P_2 :', (pAllowed |= P_2).toString(2), pAllowed);
console.log('pAllowed |= P_3 :', (pAllowed |= P_3).toString(2), pAllowed);
console.log('pAllowed |= P_4 :', (pAllowed |= P_4).toString(2), pAllowed);
console.log('pAllowed |= P_5 :', (pAllowed |= P_5).toString(2), pAllowed);
console.log('pAllowed |= P_6 :', (pAllowed |= P_6).toString(2), pAllowed);
console.log('pAllowed |= P_7 :', (pAllowed |= P_7).toString(2), pAllowed);
console.log('pAllowed 对应位赋值权限后:', pAllowed.toString(2), pAllowed);
console.log('\n取消对应权限:');
console.log('pAllowed &= ~P_1 :', (pAllowed &= ~P_1).toString(2), pAllowed);
console.log('pAllowed &= ~P_2 :', (pAllowed &= ~P_2).toString(2), pAllowed);
console.log('pAllowed &= ~P_3 :', (pAllowed &= ~P_3).toString(2), pAllowed);
console.log('pAllowed &= ~P_4 :', (pAllowed &= ~P_4).toString(2), pAllowed);
console.log('pAllowed &= ~P_5 :', (pAllowed &= ~P_5).toString(2), pAllowed);
console.log('pAllowed &= ~P_6 :', (pAllowed &= ~P_6).toString(2), pAllowed);
console.log('pAllowed &= ~P_7 :', (pAllowed &= ~P_7).toString(2), pAllowed);
console.log('pAllowed 对应位取消权限后:', pAllowed.toString(2), pAllowed);
console.log('\n同时赋值多个权限:');
console.log('pAllowed |= (P_1 | P_2 | P_3 | P_4 | P_5 | P_6 | P_7) :',
(pAllowed |= (P_1 | P_2 | P_3 | P_4 | P_5 | P_6 | P_7)).toString(2), pAllowed);
console.log('\n同时取消多个权限:');
console.log('pAllowed &= ~(P_1 | P_2 | P_3 | P_4 | P_5 | P_6 | P_7) :',
(pAllowed &= ~(P_1 | P_2 | P_3 | P_4 | P_5 | P_6 | P_7)).toString(2), pAllowed);