Wednesday, November 9, 2011 No. 20 - Number of 1 in a Binary Problem: Please implement a function to get the number of 1s in an integer. For example, the integer 9 is 1001 in binary, so it returns 2 since there are two bits of 1s. Analysis: It looks like a simple question about binary numbers, and we have many solutions for it. Unfortunately, the most intuitive solution for many candidates is incorrect. We should be careful. Solution 1: Check the most right bit, possibly with endless loop When candidates are interviewed with this problem, many of them find a solution in short time:
Read full article from Coding Interview Questions: No. 20 - Number of 1 in a Binary
No comments:
Post a Comment