To check if a number is a power of 4.

PHOTO EMBED

Sat Apr 06 2024 06:09:44 GMT+0000 (Coordinated Universal Time)

Saved by @hiimsa #java

public boolean isPowerOfFour(int n) {
        // If a number is power of 2, It will have 1 bit at even position and if its a power of 4, 
        // which is not a power of 2 e.g. 128 it will have 1 bit at odd position.

        return (n >0) && ((n &(n-1)) ==0) && ((n & 0xaaaaaaaa) ==0);
        
    }
content_copyCOPY