简单介绍下一些有用的位操作
('a' | ' ') = 'a'
('A' | ' ') = 'a'
('b' & '_') = 'B'
('B' & '_') = 'B'
('d' & ' ') = 'D'
('D' & ' ') = 'd'
const x = -1, y = 2;
console.log((x ^ y) < 0)
const x = 1, y = 2;
console.log((x ^ y) < 0)
let a = 1, b = 2;
a ^= b;
b ^= a;
a ^= b;
console.log(a, b);
let n = 1;
n = -~n;
let n = 2;
n = ~-n;
n&(n-1)
2^0 = 1 = 0b0001
2^1 = 2 = 0b0010
2^2 = 4 = 0b0100
const isPowerOfTwo(n) {
if(n<0) return fasle
return (n & (n - 1)) == 0;
}
function singleNumber(nums) {
let res = 0;
for(const n of nums) {
res ^= num;
}
return res;
}
拿去装逼吧 ^ _ ^ !
|