Programmer interview question selection (22): The number of 1 in the Binary Expression of an integer

Source: Internet
Author: User

Question: enter an integer to calculate the number of 1 in the Binary Expression of this integer. For example, input 10. Because the binary value is 1010 and there are two 1 s, Output 2.

Analysis: this is a basic examination bit operation interview question. Many companies, including Microsoft, have used this question.

The basic idea is to first judge whether the rightmost digit of an integer is 1. Next, shift the integer one to the right. The number in the second place on the right is now moved to the first place, and then judge whether it is 1. In this way, one digit is moved until the integer is 0. Now the question is how to judge whether the rightmost digit of an integer is 1. It is very simple if it and integer 1 are used for and calculation. Because 1 except the rightmost one, all other bits are 0. Therefore, if the result is 1, the rightmost digit of the integer is 1, otherwise it is 0.

The Code is as follows:

//////////////////////////////////////// ///////////////////////////////
// Get how many 1 s in an integer's Binary Expression
//////////////////////////////////////// ///////////////////////////////
Int numberof1_solution1 (int I)
{
Int COUNT = 0;
While (I)
{
If (I & 1)
Count ++;

I = I> 1;
}

Return count;
}

Some readers may ask that the first digit to the right of an integer is equivalent to dividing by 2. Can I change the right shift operator in the above Code to divide it by 2? The answer is that it is best not to replace it with Division. The Division efficiency is much lower than the shift operation. In actual programming, we should try to replace multiplication and division with shift operators as much as possible.

This idea is correct when I is a positive number, but when I is a negative number, it will lead to an endless loop. Taking the negative 0x80000000 as an example, when one digit is shifted to the right, it is not simply to move 1 of the highest bit to the second digit to 0x40000000, but 0xc0000000. This is because it is a negative number before the shift, and it is still necessary to ensure that it is a negative number after the shift, so the highest bit after the shift will be set to 1. If you continue to perform the right shift operation, the final number will become 0xffffffff and fall into an endless loop.

To avoid endless loops, we can move the input number I right away. First, perform operations on I and 1 to determine if the I percentile is 1. Then, shift 1 to the left to get 2, and then perform operations with I to determine whether the secondary high of I is 1 ...... In this way, the Left shift is repeated, and each time you can judge whether one of the I is 1. Based on this, we get the following code:

//////////////////////////////////////// ///////////////////////////////
// Get how many 1 s in an integer's Binary Expression
//////////////////////////////////////// ///////////////////////////////
Int numberof1_solution2 (int I)
{
Int COUNT = 0;
Unsigned int flag = 1;
While (FLAG)
{
If (I & flag)
Count ++;

Flag = Flag <1;
}

Return count;
}

Another idea is that if an integer is not 0, at least one of the integers is 1. If we subtract 1 from this integer, the first 1 on the rightmost side of the integer will become 0, and the first 0 after 1 will become 1. All other bits will not be affected. For example, if a binary number is 1100, the third digit from the right is 1 on the rightmost side. After 1 is subtracted, the third digit becomes 0, and the two digits after it become 1, while the first one remains unchanged. Therefore, the result is 1011.

We found that the result of 1 reduction is to reverse all bits starting from 1 on the rightmost side. At this time, if we calculate the original integer and the result after subtracting 1, all the bits will change from the first digit on the rightmost side of the original integer to 0. For example, 1100 & 1011 = 1000. That is to say, if an integer is subtracted from 1 and then compared with the original integer, the rightmost 1 of the integer is changed to 0. So how many times can this operation be performed for the binary value of an integer of 1.

The code corresponding to this idea is as follows:

//////////////////////////////////////// ///////////////////////////////
// Get how many 1 s in an integer's Binary Expression
//////////////////////////////////////// ///////////////////////////////
Int numberof1_solution3 (int I)
{
Int COUNT = 0;

While (I)
{
++ Count;
I = (I-1) & I;
}

Return count;
}

Extension: How can I use a statement to determine whether an integer is an integer power of two?

 

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.