Topic
Enter an integer that outputs the number of 1 in the binary representation. Where negative numbers are expressed in complement.
Ideas
Subtracting an integer by 1, and then doing and operating with the original integer, will change the rightmost 1 of the integer to 0. Then the number of 1 in the binary representation of an integer can be done several times.
Code
/*---------------------------------------* Date: 2015-07-20* sjf0115* title: 12. Number of 1 in binary * result: ac* URL: Http://www.now Coder.com/books/coding-interviews/8ee967e43c2c4ec193b040ea7fbb10b8?rp=1* Source: Sword Finger offer* Blog:-----------------------------------------* *#include <iostream>#include <vector>#include <string>#include <stack>#include <algorithm>using namespace STD;classSolution { Public:intNUMBEROF1 (intN) {intCount =0; while(n) {n &= (n-1); ++count; }//while returnCount }};intMain () {solution S;intN while(Cin>>n) {intresult = S.NUMBEROF1 (n);//Output cout<<result<<endl; }//while return 0;}
Two ideas
We may soon have a way of thinking: First of all integer binary representations of the rightmost one is not 1. Then move the input integer right one bit, at which point the second position is shifted from the right to the far right, and then the judgment is 1. This moves one bit at a time until the whole integer becomes 0. Based on this idea, we have finished writing the following procedure. But when we enter a negative number, there is a problem with this method. For example, 0x80000000, the negative 0x80000000 to the right one is not simply to move the top 1 to the second position to become 0x40000000, but 0xc0000000. This is because before the shift is a negative number, still to ensure that the shift is a negative number, so the highest post shift is still 1. If you do a right-shift operation, the number will eventually become 0xFFFFFFFF and fall into a dead loop .
Code two
class Solution {public: int NumberOf1(int n){ intcount0; while(n){ if1){ ++count; }//if 1; }//while returncount; }};
Three ideas
To avoid a dead loop, we can not shift the input number n right. First, N and 1 are done with the operation, to determine whether the lowest bit of n is 1. Then the 1 left one to get 2, and then n to do with the operation, you can determine the second low of n is not 1 ... so repeatedly left, you can judge one of n is not 1.
Code Three
/*---------------------------------------* Date: 2015-07-20* sjf0115* title: 12. Number of 1 in binary * result: ac* URL: Http://www.now Coder.com/books/coding-interviews/8ee967e43c2c4ec193b040ea7fbb10b8?rp=1* Source: Sword Finger offer* Blog:-----------------------------------------* *#include <iostream>#include <vector>#include <string>#include <stack>#include <algorithm>using namespace STD;classSolution { Public:intNUMBEROF1 (intN) {intCount =0;intBase =1; while(base) {if(N & Base) {++count; }//ifBase = Base <<1; }//while returnCount }};intMain () {solution S;intN while(Cin>>n) {intresult = S.NUMBEROF1 (n);//Output cout<<result<<endl; }//while return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Sword refers to offer]12. Number of 1 in binary