Friday, September 24, 2010

to count the number of "1" for any input integer

first asking for an input, and transfer it into binary, then count the number of "1"
    int func(x)     {    
           int countx = 0;    
           while(x)      {      
                    countx ++;      
                    x = x&(x-1);      
           }    
           return countx;    
       }

No comments:

Post a Comment