Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 758 Bytes

File metadata and controls

43 lines (32 loc) · 758 Bytes

Method 1 (Recursion)

 bool isPowerOfFour(int n) {
        if(n==0) return false;
        if(n==1) return true;

        if(n%4 !=0)    return false;

        return isPowerOfFour(n/4);
    }

Method 2 (Bit Manipulatiion)

     int lsb(int n)
    {
        int p=0;

        while(n>0)
        {
            if(n&1 > 0)
                return p;
            n=n>>1;
            p++;
        }
        return -1;
    }
    bool isPowerOfFour(int n) {
        if(n==0) return false;

        if(n==1) return true;

        if(lsb(n)%2==0 && (n&(n-1))==0)
            return true;
        return false;
        
            }