Bit Manipulation
x >> 1
x << 1
x & 1
- reverse bit
int hi = 31, lo = 0; while (hi > lo) { // swap bit int x = (n >> hi) & 1; int y = (n >> lo) & 1; if (x != y) { // use '^' to reverse bit n ^= (1 << hi) | (1 << lo); } hi--; lo++; }
- use bit to store multiple states to reduce space complexity
- use ‘^’ to seperate two number, find the first ‘1’ position
- condition check
- (expression A) ^ (expressiion B), to check if two expressions has different results