位运算中可以使用的三个技巧
- x & (-x) to keep the rightmost 1-bit and to set all the others bits to zero.
application:
- x&(-x)==x check if x is power of 2, pay attention to potentially overflow of -x
- x & (x - 1) set the rightmost 1 bit as 0.
application:
- x&(x - 1)==0 check if x is power of 2
- x % 2 or x & 1 to retrive the right most bit

浙公网安备 33010602011771号