Complete the code to check if a number is a power of two using bitwise AND.
function isPowerOfTwo(n: number): boolean {
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 immediately if the number is not positive.
function isPowerOfTwo(n: number): boolean {
if ([1]) return false;
return (n & (n - 1)) === 0;
}Numbers less than or equal to zero cannot be powers of two.
Fix the error in the bitwise check to correctly identify powers of two.
function isPowerOfTwo(n: number): boolean {
if (n <= 0) return false;
return (n & [1]) === 0;
}The correct check is (n & (n - 1)) === 0.
Fill both blanks to create a function that returns true if n is a power of two, false otherwise.
function isPowerOfTwo(n: number): boolean {
return n [1] 0 && (n & (n [2] 1)) === 0;
}The function checks if n is greater than zero and uses n & (n - 1) to verify power of two.
Fill all three blanks to create a function that returns true if n is a power of two, false otherwise.
function isPowerOfTwo(n: number): boolean {
if (n [1] 0) return false;
return (n & (n [2] 1)) [3] 0;
}The function returns false if n is less than or equal to zero, then checks if n & (n - 1) === 0.
