标签:
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; } }
Jan 12 - Power of Two; Integer; Bit Manipulation;
标签:
原文地址:http://www.cnblogs.com/5683yue/p/5126123.html