Bird
Raised Fist0

What is the output of the following code when the input is 11?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Number of 1 Bits (Hamming Weight)
What is the output of the following code when the input is 11?
def hammingWeight(n: int) -> int:
    return bin(n).count('1')

print(hammingWeight(11))
A2
B4
C3
D1
Step-by-Step Solution
  1. Step 1: Convert 11 to binary

    11 in binary is 1011.
  2. Step 2: Count '1's in '1011'

    There are three '1's in the string.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    bin(11) = '0b1011', count('1') = 3 [OK]
Quick Trick: Count '1's in binary string representation [OK]
Common Mistakes:
MISTAKES
  • Counting bits incorrectly
  • Confusing decimal and binary digits
Trap Explanation:
PITFALL
  • Off-by-one errors in counting bits or misreading binary string length.
Interviewer Note:
CONTEXT
  • Checks ability to trace built-in function usage and understand binary representation.
Master "Number of 1 Bits (Hamming Weight)" 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