Complete the code to check if a number is a power of two using bitwise AND.
bool isPowerOfTwo(int n) {
return n > 0 && (n & [1]) == 0;
}The expression n & (n - 1) clears the lowest set bit. For powers of two, this results in zero.
Complete the code to return false if the number is zero.
bool isPowerOfTwo(int n) {
if ([1]) return false;
return (n & (n - 1)) == 0;
}Zero is not a power of two, so we return false if n is zero.
Fix the error in the return statement to correctly check power of two.
bool isPowerOfTwo(int n) {
if (n == 0) return false;
return (n & [1]) == 0;
}The correct check uses n - 1 to clear the lowest set bit.
Fill both blanks to complete the function that returns true if n is a power of two and false otherwise.
bool isPowerOfTwo(int n) {
if ([1]) return false;
return (n & [2]) == 0;
}We first check if n is less than or equal to zero, then use n & (n - 1) to check power of two.
Fill all three blanks to complete the function that returns true if n is a power of two, false otherwise.
bool isPowerOfTwo(int n) {
if ([1]) return false;
return ([2] & [3]) == 0;
}Check if n is less than 1, then use n & (n - 1) to verify if n is a power of two.
