
338. Counting Bits
- method 1
Bit operator
-
<<, >>
- x << y : shift bits in the literal left by 3
- 3 = 0011, 3 << 2 = 0110 = 6, 3 << 3 = 1000
- x >> y: shift bits in the literal x right by y
- 12 = 1100, 12 >> 3 = 0001
- x << y : shift bits in the literal left by 3
-
^
- 0 ^ arbitrary number = arbitrary number
- 1 ^ 0 = 1 1 ^ 1 = 0 usually used to calculate the number of 1 in a number
calculate every number in the range
1 |
int calculate(int n){ |
- method 2-dp
find the pattern 🙂




近期评论