Two‘s complement of integer:
https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8
Bit Manipulation:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
代码:
public class Solution { public boolean isPowerOfTwo(int n) { int count = 0; if((n & Integer.MIN_VALUE) != 0){ //n = ~n + 1; return false; } for(int i = 32; i > 0; i--){ count += n & 1; n = n>>1; } return count == 1; } }
时间: 2024-10-08 20:41:48