📖 Scenario: Imagine you are working with a system that needs to quickly check if certain numbers are powers of two. Powers of two are numbers like 1, 2, 4, 8, 16, and so on. This is useful in computer memory allocation and optimization.
🎯 Goal: You will build a simple TypeScript program that checks if a given number is a power of two using a clear step-by-step approach.
📋 What You'll Learn
Create a variable to store the number to check
Create a helper variable to hold the result
Write the core logic to check if the number is a power of two
Print the result as true or false
💡 Why This Matters
🌍 Real World
Checking if a number is a power of two is useful in computer memory management and optimization tasks.
💼 Career
Understanding bitwise operations and condition checks is important for software developers working with low-level programming and performance optimization.
Progress0 / 4 steps
1
Create the number variable
Create a variable called num and set it to 16.
DSA Typescript
Hint
Use const to create the variable num and assign the value 16.
2
Create the result variable
Create a variable called isPowerOfTwo and set it to false.
DSA Typescript
Hint
Use let to create isPowerOfTwo and start with false.
3
Check if the number is a power of two
Write an if statement that sets isPowerOfTwo to true if num is greater than 0 and (num & (num - 1)) === 0.
DSA Typescript
Hint
This bitwise trick checks if only one bit is set in num. Remember to check num & (num - 1) equals zero.
4
Print the result
Write console.log(isPowerOfTwo) to print the result.
DSA Typescript
Hint
Use console.log to show the value of isPowerOfTwo.
Practice
(1/5)
1. What does it mean if a number is a power of two?
easy
A. It is divisible by 3
B. It is always an odd number
C. It can be written as 2 raised to some integer exponent
D. It is a prime number
Solution
Step 1: Understand the definition of power of two
A number is a power of two if it equals 2 raised to an integer exponent, like 2^0=1, 2^1=2, 2^2=4, etc.
Step 2: Check other options
Odd numbers, divisibility by 3, or primality do not define powers of two.
Final Answer:
It can be written as 2 raised to some integer exponent -> Option C
Quick Check:
Power of two = 2^n [OK]
Hint: Power of two means 2 multiplied by itself n times [OK]
Common Mistakes:
Thinking all powers of two are odd
Confusing powers of two with prime numbers
Assuming divisibility by 3 means power of two
2. Which of the following TypeScript expressions correctly checks if a number n is a power of two using bitwise operations?
easy
A. return n > 0 && (n & (n - 1)) === 0;
B. return n > 0 && (n | (n - 1)) === 0;
C. return n > 0 && (n & (n + 1)) === 0;
D. return n > 0 && (n ^ (n - 1)) === 0;
Solution
Step 1: Understand the bitwise check for power of two
The expression n & (n - 1) clears the lowest set bit. For powers of two, this results in zero.
Step 2: Analyze each option
return n > 0 && (n & (n - 1)) === 0; uses the correct bitwise AND with (n-1). Options A, B, and C use incorrect bitwise operators or operands.
Final Answer:
return n > 0 && (n & (n - 1)) === 0; -> Option A
Quick Check:
Bitwise AND with (n-1) = 0 means power of two [OK]
Hint: Use n & (n-1) === 0 to check power of two [OK]
Common Mistakes:
Using bitwise OR instead of AND
Using (n + 1) instead of (n - 1)
Forgetting to check n > 0
3. What will be the output of this TypeScript code?
function isPowerOfTwo(n: number): boolean {
return n > 0 && (n & (n - 1)) === 0;
}
console.log(isPowerOfTwo(16));
console.log(isPowerOfTwo(18));
medium
A. true true
B. false true
C. false false
D. true false
Solution
Step 1: Check isPowerOfTwo(16)
16 in binary is 10000. 16 & 15 (01111) = 0, so returns true.
Step 2: Check isPowerOfTwo(18)
18 in binary is 10010. 18 & 17 (10001) = 16 (not zero), so returns false.
Final Answer:
true false -> Option D
Quick Check:
16 is power of two, 18 is not [OK]
Hint: Check bitwise AND with (n-1) to confirm power of two [OK]
Common Mistakes:
Assuming 18 is power of two because it's even
Ignoring the n > 0 condition
Misreading bitwise AND results
4. Identify the error in this TypeScript function that checks if a number is a power of two: