🧠
Bit Manipulation Using Power of Two Check and Bitmask
💡 This approach leverages the binary properties of powers of four, combining a power of two check with a mask to ensure the single set bit is in the correct position.
Intuition
A power of four is a power of two with its single set bit at an even position (0-based). First check if n is a power of two, then check if the set bit is in an even position using a mask.
Algorithm
- Check if n is positive and n & (n-1) == 0 to confirm power of two.
- Use mask 0x55555555 to check if the set bit is at an even position.
- Return true if both conditions hold, else false.
💡 This approach is efficient and uses constant time bitwise operations.
def isPowerOfFour(n: int) -> bool:
if n <= 0:
return False
if (n & (n - 1)) != 0:
return False
if (n & 0x55555555) == 0:
return False
return True
# Driver code
if __name__ == '__main__':
test_cases = [16, 5, 1, 0, -4]
for tc in test_cases:
print(f'isPowerOfFour({tc}) = {isPowerOfFour(tc)}')
Line Notes
if n <= 0:Exclude zero and negative numbers early
if (n & (n - 1)) != 0:Check if n is a power of two by verifying only one bit is set
if (n & 0x55555555) == 0:Check if the set bit is in an even position using mask
return TrueReturn true only if both conditions are met
public class PowerOfFour {
public static boolean isPowerOfFour(int n) {
if (n <= 0) return false;
if ((n & (n - 1)) != 0) return false;
if ((n & 0x55555555) == 0) return false;
return true;
}
public static void main(String[] args) {
int[] testCases = {16, 5, 1, 0, -4};
for (int tc : testCases) {
System.out.println("isPowerOfFour(" + tc + ") = " + isPowerOfFour(tc));
}
}
}
Line Notes
if (n <= 0) return false;Filter out invalid inputs
if ((n & (n - 1)) != 0) return false;Check single set bit for power of two
if ((n & 0x55555555) == 0) return false;Mask to ensure set bit is at even position
return true;Return true if all conditions satisfied
#include <iostream>
using namespace std;
bool isPowerOfFour(int n) {
if (n <= 0) return false;
if ((n & (n - 1)) != 0) return false;
if ((n & 0x55555555) == 0) return false;
return true;
}
int main() {
int testCases[] = {16, 5, 1, 0, -4};
for (int tc : testCases) {
cout << "isPowerOfFour(" << tc << ") = " << (isPowerOfFour(tc) ? "true" : "false") << endl;
}
return 0;
}
Line Notes
if (n <= 0) return false;Exclude zero and negatives
if ((n & (n - 1)) != 0) return false;Check if n is power of two
if ((n & 0x55555555) == 0) return false;Mask to verify bit position
return true;Return true if all checks pass
function isPowerOfFour(n) {
if (n <= 0) return false;
if ((n & (n - 1)) !== 0) return false;
if ((n & 0x55555555) === 0) return false;
return true;
}
// Test cases
[16, 5, 1, 0, -4].forEach(tc => {
console.log(`isPowerOfFour(${tc}) = ${isPowerOfFour(tc)}`);
});
Line Notes
if (n <= 0) return false;Exclude invalid inputs
if ((n & (n - 1)) !== 0) return false;Check single set bit for power of two
if ((n & 0x55555555) === 0) return false;Mask to ensure set bit is at even position
return true;Return true if all conditions met
All operations are constant time bitwise operations, independent of input size.
💡 This means the function runs instantly even for very large numbers.
Interview Verdict: Accepted
This is the optimal and most elegant solution for this problem.