Bit Manipulation - Power of FourIdentify the bug in the following code snippet that attempts to check if a number is a power of four:AUsing 'log_val == int(log_val)' causes false negatives due to floating point precisionBThe condition 'if n <= 0' should be 'if n < 0' to allow zeroCThe division by 2 should be multiplication by 0.5 for better precisionDThe function should use a loop instead of logarithm for accuracyCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand floating point precision issueFloating point calculations can introduce tiny errors, so direct equality check can fail for valid powers of four.Step 2: Identify the bugThe line 'return log_val == int(log_val)' is the bug because it does not allow for a tolerance margin.Final Answer:Option A -> Option AQuick Check:Adding a tolerance check like abs(log_val - int(log_val)) < epsilon fixes the bug [OK]Quick Trick: Floating point equality without tolerance causes subtle bugsCommon Mistakes:MISTAKESIgnoring floating point errorsMisusing inequality conditions for input validationTrap Explanation:PITFALLCandidates often trust direct equality on floats, missing precision issues that cause rare failures.Interviewer Note:CONTEXTChecks candidate's awareness of floating point pitfalls in mathematical checks.
Master "Power of Four" in Bit Manipulation3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Bit Manipulation Quizzes Find Two Missing Numbers - Find Two Missing Numbers - Quiz 14medium Maximum XOR of Two Numbers - Maximum XOR of Two Numbers - Quiz 8hard Reverse Bits - Reverse Bits - Quiz 7medium Single Number (XOR Self-Cancellation) - Single Number (XOR Self-Cancellation) - Quiz 13medium Single Number (XOR Self-Cancellation) - Single Number (XOR Self-Cancellation) - Quiz 4medium Single Number (XOR Self-Cancellation) - Single Number (XOR Self-Cancellation) - Quiz 15hard Single Number (XOR Self-Cancellation) - Single Number (XOR Self-Cancellation) - Quiz 10hard Single Number II (Three Copies) - Single Number II (Three Copies) - Quiz 7medium Single Number III (Two Singles) - Single Number III (Two Singles) - Quiz 12easy UTF-8 Validation - UTF-8 Validation - Quiz 3easy