1. 程式人生 > >leetcode Power of Two

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;
		}
	}