位与运算的性质:
- x & (−x) 可以获得 x 的二进制表示中的最低位的 1 的位置;
( -x = x的反码+1) - x & (x−1) 可以将 x 的二进制表示中的最低位的 1 置成 0。
- 2^(n) 用 java 写是 (1<<n): 相当于将1左移 n 位
LeetCode: 51
bitCount(): method of Integer class of java.lang package returns the count of the number of one-bits in the two’s complement binary representation of an int value.
// java bitCount():
import java.lang.Integer;
class Gfg {
public static void main(String args[])
{
int a = 10;
// Convert integer number to binary format
System.out.println(Integer.toBinaryString(a));
// to print number of 1's in the number a
System.out.println(Integer.bitCount(a));
}
}
Output:
1010
2