Calculates the number of binary 1 in an integer, and the number of binary 1 in an integer.
Question: evaluate an integer binary to represent the number of 1
First version:
Idea: if an integer is compared with 1, and the result is 1, the rightmost digit of the integer is 1; otherwise, the result is 0;
Int NumberOf1 (int n) {int count = 0; while (n) {if (n & 1) // if the result of an integer and 1 is 1, indicates that the rightmost side of the integer is 1; otherwise it is 0; {count ++;} n = n> 1;} return count ;}
Disadvantage: because there is a right shift in the Code, when it is a negative number, you need to consider the symbol bit; if a positive number, after the right shift, add n zeros to the leftmost; if the number was originally a negative number, then, after the right shift, add n 1 at the leftmost.
The last loop will lead to an endless loop.
Version 2:
Ideas:
First, perform operations on n and 1 to determine whether the nth percentile is 1. Next, shift 1 to the left to get 2, and then perform the operation with n to determine whether the next low position of n is 1... This way, the Left shift is repeated, and each time you can judge
Is one of n 1.
In this solution, the number of cycles is equal to the number of digits in the integer binary, and the 32-bit integer needs to be cyclically 32 times.
int NumberOf1Ex(int n){int count = 0;unsigned int key = 1;while (key){if (n & key){count++;}key = key<<1;}return count;}
Disadvantages:
In this solution, the number of cycles is equal to the number of digits in the integer binary, and the 32-bit integer needs to be cyclically 32 times.
Third Edition:
After subtracting 1 from an integer and then performing an operation with the original integer, the rightmost 1 of the integer is changed to 0. Then, the binary representation of an integer contains
You can perform this operation as many as one.
int NumberOf1Ex2(int n){int count = 0;while (n){n = n & (n-1);++count;}return count;}
Complete Test code:
// GetOneNumber. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std; int NumberOf1 (int n) {int count = 0; while (n) {if (n & 1) // if the result of an integer and 1 is 1, it indicates that the rightmost side of the integer is 1, otherwise it is 0; {count ++ ;} n = n> 1;} return count;}/* First, perform operations on n and 1 to determine if the nth bit of n is 1. Next, shift 1 to the left to get 2, and then perform the operation with n to determine whether the next low position of n is 1 .... in this way, the Left shift is repeated, and each time you can determine whether one of n is 1. in this solution, the number of cycles is equal to the number of digits in the integer binary, and the 32-bit integer needs to be cyclically 32 times. */Int NumberOf1Ex (int n) {int count = 0; unsigned int key = 1; while (key) {if (n & key) {count ++ ;} key = key <1;} return count;}/* principle: Subtract 1 from an integer, and then perform operations with the original integer, it will change the rightmost 1 of the integer to 0. Then, the number of such operations can be performed for the number of 1 in the binary representation of an integer. */Int NumberOf1Ex2 (int n) {int count = 0; while (n) {n = n & (n-1); ++ count;} return count ;} int _ tmain (int argc, _ TCHAR * argv []) {cout <NumberOf1Ex (9) <endl; cout <NumberOf1Ex2 (12) <endl; cout <NumberOf1Ex (-1) <endl; getchar (); return 0 ;}