Skip to content

Instantly share code, notes, and snippets.

@kelvin-fly
Created October 26, 2012 02:43
Show Gist options
  • Select an option

  • Save kelvin-fly/3956609 to your computer and use it in GitHub Desktop.

Select an option

Save kelvin-fly/3956609 to your computer and use it in GitHub Desktop.
get the sum of number's 1
#include <stdio.h>
#include <math.h>
int num_1( int value, int n_bits);
int main()
{
int a, b, c;
scanf("%o",&a);
scanf("%d",&b);
c = num_1( a, b );
printf("%d\n", c);
return 0;
}
int num_1(int value, int n_bits )
{
int parity = 0;
while(n_bits > 0)
{
parity += value & 1;
value >>= 1;
n_bits -=1;
}
return parity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment