Java solution using bit maniputation


  • 0
    V

    public class Solution {

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

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.