One algorithm question every day: the number of digits in the binary format of 1

Source: Internet
Author: User

Question: implement a function that belongs to an integer and outputs the number of 1 in the binary representation. For example, if 9 is expressed as 1001 in binary notation, two digits are 1. Therefore, if 9 is input, the function outputs 2.

  • Possible endless loop traps

After reading the questions, I believe that you will soon be able to think of a solution: first, judge whether the rightmost digit in the integer Binary Expression is 1, and then shift the input integer to the right, at this time, the second digit from the right is moved to the rightmost, and then 1 is determined, so that one digit is moved every time until the integer is changed to 0, that is to say, we can get the number of 1 in the integer binary representation. But now the question is how to judge the last digit of a number as 1. In fact, this is also very simple, only the number and 1 are involved in the operation. If the result is 1, the last digit is 1. Otherwise, the value is 0. With the above ideas, we can quickly write the following code:

 1 int NumberOf1(int number) 2 { 3     int count = 0; 4      5     while (0 != number) 6     { 7         if (0 != (number & 0x01)) 8         { 9             ++count;10         }11 12         number = number >> 1;13     }    14 15     return count;16 }

As a rigorous programmer, you must test the code after writing the code. When we enter a positive integer, the program can run correctly, but when we enter a negative integer, it seems that the program cannot be stopped. Why? When we look back at the code and see number = number> 1, we suddenly realized that because a negative number carries a signed bit, when performing the right shift operation, we should keep it as a negative number, in this way, the first digit on the left is supplemented with 1 rather than 0, so that the final integer is changed to 0 xffffffff, and the result is in an endless loop. How can this problem be avoided?

  • General Solution

When the above problem occurs, we think that since we can't move the integer to the right, why don't we shift 1 to the left and then compress it with the input integer? This processing method can achieve the same effect, and the left shift will always add 0 in the right position, so as to avoid the emergence of an endless loop, with this idea, we can write the following second version code:

 1 int NumberOf1(int number) 2 { 3     int count = 0; 4     int flag = 0x01; 5  6     while (0 != flag) 7     { 8         if (0 != (number & flag)) 9         {10             ++count;11         }12 13         flag = flag << 1;14     }15 16     return count;17 }

After the code is complete, all tests can get the correct results. It seems to be a perfect answer, but we find that no matter how many of the input integer binary forms are 1, our programs can get results after 32 loops. Is there any way to reduce the number of loops? For example, if there are several methods in the binary system that can be iterated for only a few times, the answer is yes. Please go on and look down.

  • Amazing Solution

Before analyzing the algorithm, we will first analyze the situation where an integer is reduced by 1. If an integer is not equal to 0, the binary representation of this integer must have at least one digit as 1, assume that the last digit of this number is 1. After this integer is reduced by 1, the last digit is changed to 0, and the other digits remain unchanged, next, let us assume that the last digit is not 1 but 0. If the rightmost 1 of the number is in the m position, we will subtract this number by 1, the M-bit of the number changes from 1 to 0, and all the bits after the M-bit change from 0 to 1. The bits before the M-bit remain unchanged, if you subtract the result from the integer and the integer by 1, the result is equivalent to changing the bitwise of the rightmost integer from 1 to 0. The above analysis is summarized as follows: after an integer is reduced by 1 and then compared with the integer itself, the rightmost 1 in the integer binary form is changed to 0, then we can perform the preceding operations several times on the integer in the binary form. With the above ideas, we can write a new solution:

 1 int NumberOf1(int number) 2 { 3     int count = 0; 4  5     while (0 != number) 6     { 7         ++count; 8  9         number = number & (number - 1);10     }11 12     return count;13 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.