leetcode Power of Two

Power of Two 题目:https://leetcode.com/problems/power-of-two/

Power of Two  判断一个数是否是2的整数次幂

public static void main(String[] args) {
		int n=127;
		boolean powerOfTwo = isPowerOfTwo(n);
		System.out.println(powerOfTwo);
	}

	public static boolean isPowerOfTwo(int n) {
		int result=0;
		if(n<0){
			return false;
		}
		while(n!=0){
			result+=n&1;
			n>>=1;
		}
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/84871787
今日推荐