Bird
Raised Fist0

Given the following code, what is the output of isPowerOfTwo(16)?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - Power of Two
Given the following code, what is the output of isPowerOfTwo(16)?
AError
BFalse
CTrue
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate input 16

    16 in binary is 10000. 16 - 1 = 15 (01111).
  2. Step 2: Compute n & (n - 1)

    10000 & 01111 = 00000, which equals 0, so function returns True.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    16 is a power of two, so output is True [OK]
Quick Trick: n & (n-1) == 0 means power of two [OK]
Common Mistakes:
MISTAKES
  • Miscompute bitwise AND
  • Forget to check n > 0
Trap Explanation:
PITFALL
  • Candidates sometimes confuse bitwise AND result or forget zero check.
Interviewer Note:
CONTEXT
  • Tests ability to trace bitwise operations on simple input.
Master "Power of Two" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes