Complete the code to check if a number is a power of two using bitwise AND.
int isPowerOfTwo(unsigned int n) {
return n && !(n & (n [1] 1));
}The expression n & (n - 1) clears the lowest set bit. If the result is zero, n is a power of two.
Complete the code to return 1 if n is power of two, else 0.
int isPowerOfTwo(unsigned int n) {
if (n == 0) return 0;
return (n & (n [1] 1)) == 0;
}Subtracting 1 from n flips the lowest set bit and all bits after it. The bitwise AND with n should be zero for powers of two.
Fix the error in the code to correctly check if n is power of two.
int isPowerOfTwo(unsigned int n) {
if (n == 0) return 0;
return (n & (n [1] 1)) == 0;
}The correct operation is subtraction. Using addition or multiplication will not correctly clear the lowest set bit.
Fill both blanks to create a function that returns 1 if n is power of two, else 0.
int isPowerOfTwo(unsigned int n) {
if (n == [1]) return 0;
return (n & (n [2] 1)) == 0;
}We check if n is zero first, then use subtraction in the bitwise AND expression.
Fill all three blanks to complete the function that returns 1 if n is power of two, else 0.
int isPowerOfTwo(unsigned int n) {
if (n == [1]) return [2];
return (n & (n [3] 1)) == 0;
}Check if n is zero and return 0. Then use subtraction in the bitwise AND check.
