How many integers are in the binary notation of 1?

Source: Internet
Author: User

Reprinted from:Http://www.cnblogs.com/python27/.

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 1 s 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 1 s 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.

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.