Bitmask Session
Bitmask Session
& and
| or
^ xor
! not
~ 1’s complement
>> shift right
x >> n = x / (2 ^ n)
5 (00101)<< 2 = 20 (10100)
x << n = x * (2 ^ n)
Tricks:
a^b=c
a^c=b
c^b=a
So you can do partial xor:
for(int i = 1; i <= n; ++i) xor[i] = a[i] ^ xor[i - 1];
operations on bits:
// Everything is 0 based counted from the right
/* built in fuctions */
bitset<10> b(5); // 10 is the size
cout<<b<<'\n'; // print 0000000101
b[0] = 0;
cout<<b<<'\n'; // print 0000000100
b.set();
cout<<b<<'\n'; // print 1111111111
b.flip(); //reverse all bits
cout<<b<<'\n'; // print 0000000000
b.flip(2);
cout<<b<<'\n'; // print 0000000100
__builtin_clz(n);
__builtin_clzll(n);
/* lowbit */
(n & -n) // -n is 2's compelement
int res = 0;
for(int j = 0; j < 32; ++j) int res = 0;
if(pre[r][j] - pre[l - 1][j] == r-l+1) for(int j = 0; j < 32; ++j)
res |= (1<<j); if(pre[r][j] - pre[l - 1][j])
res |= (1<<j);
cout<<ans<<' '<<res;
cout<<ans<<' '<<res;
problem
https://cses.fi/problemset/task/1623
Compelete Search
Gold Trick