Question: enter an integer to determine the number of 1 in the binary representation of the positive number? For example, if the input integer 12 is converted to a binary value of 1100, there are two 1 in total, SO 2 should be output.
Analysis 1: We can consider how to determine whether each bit is 1 from right to left? We have this number and integer 1 (01) done and operate. Because 1 except the last digit, all the other parts are 0, if the last digit of the integer is 1, the return result is 1. If the last 1 of an integer is 0, the return result is 0. Then, let the integer and 2 (10) perform the operation, then, we can determine whether the second-to-last integer is 1. Then, let the integer and the fourth (100) perform an operation to determine whether the third-to-last integer is 1. and so on, we can determine the number of all 1 integers, and the number of operations with integers is 1, 2, 4, 8 ...... we can perform the 1-bit left shift operation. Based on this idea, we can get the following code:
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 int NumbersOf1s(int number)
6 {
7 int cnt = 0;
8 unsigned int flag = 1;
9 while(flag)
10 {
11 if(flag&number)
12 cnt++;
13
14 flag = flag<<1;
15 }
16 return cnt;
17 }
18
19 int main()
20 {
21 cout<<"Enter A Number:"<<endl;
22 int i;
23 cin>>i;
24 cout<<"the numbers of 1s in your number is:"
25 <<NumbersOf1s(i)<<endl;
26 return 0;
27 }
The running result is as follows:
Analysis 2: If an integer is not 0, at least one of its binary forms is 1. If we subtract 1 from a binary number, then the rightmost 1 of the binary number will be changed to 0, and the 0 after this 1 will be changed to 1. take 1100 as an example. The rightmost 1 is the third digit of the right number. After this number is subtracted from 1, it becomes 1011. If we calculate the obtained number and original number, the first 1 on the right and the number after it are both 0, that is, 1100 & 1011 = 1000, that is, after a number is subtracted from 1, and the operation is performed on the original number, the rightmost 1 and the number of 1 will be eliminated. Then we can perform a similar loop as many times. Based on this idea, we have the following code:
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 int NumbersOf1s(int number)
6 {
7 int cnt = 0;
8 while(number)
9 {
10 cnt++;
11 number = number & (number - 1);
12 }
13
14 return cnt;
15 }
16
17 int main()
18 {
19 cout<<"Enter A Number:"<<endl;
20 int i;
21 cin>>i;
22 cout<<"The Numbers of 1s in your number is:"
23 <<NumbersOf1s(i)<<endl;
24 return 0;
25 }
The running result is as follows:
Concerning the shift operation and multiplication and division operation: In Analysis 1, we can say that we can continue to perform a one-digit operation to get the values of 1, 2, 4, 8, and 16 in decimal format. Some may say, why do we not perform the X 2 operation on the flag? Because the efficiency of the shift operation is much higher than the multiplication and division operation, it is not just a question. In other programming, we should also try to replace the multiplication and division operations with the shift operation.
References:
Programmer interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/2541117420073118945734/
Note:
1) All the code environments of this blog are compiled in win7 + VC6. All code has been debugged by the blogger.
2) The blogger python27 has the copyright to this blog post. For the reposted website, please indicate the source at http://www.cnblogs.com/python27 /. You have any suggestions for solving the problem. Please feel free to comment on them.