November 18th, 2012, 10:21 AM

Count number of "on" bits in an int
Im trying to count the number of 1's in an integer. I have tried to use ~0 and some bit manipulation to implement the algorithm but I gave up and just decided to check the lower bit and shift until the int is empty. I get a compile error about conflicting types for int_size. Any help on debugging a bit would be appreciated.
Code:
#include <stdio.h>
#include <stdbool.h>
int main(unsigned int count)
{
unsigned int input;
printf("\n\nEnter a positive integer\n\n");
scanf("%i", input);
int_size(input);
return input;
}
unsigned int int_size(unsigned int value)
{
unsigned int count = 0;
while (value > 0)
{ // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
return count;
}
November 18th, 2012, 10:53 AM

> I get a compile error about conflicting types for int_size. Any help on debugging a bit would be appreciated.
Well several things.
1. Prototype the function before trying to call it.
2. Declare main() properly
3. Use scanf() properly (%i is not for unsigned ints, and you forgot &)
4. Print the result.
November 18th, 2012, 11:15 AM

Thanks for the reply Salem, I have seen you address this same question on another forum. My scanf is fixed, and I prototyped int_size. I guess, I am still declaring main incorrectly. Should I have main with no argument at all? "main(void)"
Code:
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
int main (void)
{
int input;
printf("\n\nEnter a positive integer\n\n");
scanf("%u",&input);
unsigned int int_size(input);
return input;
}
unsigned int int_size (unsigned int value)
{
unsigned int count = 0;
while (value > 0)
{ // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
printf("\n\nNumber of on bits is: %i\n\n", count);
return count;
}
November 18th, 2012, 11:30 AM

Ive decided that main doesnt need an argument, correct me if im wrong. I can compile the following code, but I am not getting an output statement. My printf statement looks good to me. :confused:
Code:
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
int main (void)
{
unsigned int input;
printf("\n\nEnter a positive integer\n\n");
scanf("%u",&input);
unsigned int int_size(input);
return 0;
}
unsigned int int_size (unsigned int value)
{
unsigned int count = 0;
while (value > 0)
{ // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
printf("\n\nNumber of on bits is: %i\n\n", count);
return count;
}
November 18th, 2012, 11:57 AM

Fixed the argument for the int_size call. Now if I can just get it to print I can see if my algorithm works.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
int main (void)
{
unsigned int input;
printf("\n\nEnter a positive integer\n\n");
scanf("%u",&input);
unsigned int int_size(unsigned int input);
return 0;
}
unsigned int int_size (unsigned int value)
{
unsigned int count = 0;
while (value > 0)
{ // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
return count;
printf("\n\nNumber of on bits is: %d\n\n", count);
}
November 18th, 2012, 12:10 PM

Follow the comments.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
int main (void)
{
unsigned int input;
printf("\n\nEnter a positive integer\n\n");
scanf("%u",&input);
//!! yes, this is a prototype.
//!! what is missing now is a CALL to the function
unsigned int int_size(unsigned int input);
return 0;
}
unsigned int int_size (unsigned int value)
{
unsigned int count = 0;
while (value > 0)
{ // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
return count;
//!! how do you expect to print anything here?
//!! the return statement is unconditional, it never reaches the print.
printf("\n\nNumber of on bits is: %d\n\n", count);
}
November 18th, 2012, 12:25 PM

Thank you, it is working now. Not sure why I never bothered to call the function I wrote. Not paying attention to the basics.
November 18th, 2012, 05:12 PM
