Bit Manipulation - Number of 1 Bits (Hamming Weight)What is the output of the following code when the input is 0?A1B0C32DError or infinite loopCheck Answer
Step-by-Step SolutionSolution:Step 1: Check loop condition with n=0Since n is 0, the while loop condition is false immediately.Step 2: Return countCount remains 0, so function returns 0.Final Answer:Option B -> Option BQuick Check:Zero input yields zero set bits [OK]Quick Trick: Loop skips if n=0, count stays zero [OK]Common Mistakes:MISTAKESAssuming loop runs at least onceConfusing zero input with all bits setTrap Explanation:PITFALLSome candidates expect loop to run once even if n=0, leading to wrong answers.Interviewer Note:CONTEXTTests edge case handling and loop boundary understanding.
Master "Number of 1 Bits (Hamming Weight)" in Bit Manipulation3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Bit Manipulation Quizzes Bitwise AND of Numbers Range - Bitwise AND of Numbers Range - Quiz 15hard Find Two Missing Numbers - Find Two Missing Numbers - Quiz 4medium Find Two Missing Numbers - Find Two Missing Numbers - Quiz 7medium Generate All Permutations Using Bitmask DP - Generate All Permutations Using Bitmask DP - Quiz 9hard Gray Code - Gray Code - Quiz 13medium Maximum XOR of Two Numbers - Maximum XOR of Two Numbers - Quiz 5medium Power of Four - Power of Four - Quiz 5medium Power of Two - Power of Two - Quiz 7medium Reverse Bits - Reverse Bits - Quiz 15hard UTF-8 Validation - UTF-8 Validation - Quiz 13medium