#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0

    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;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    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;
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    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;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    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);
    }
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    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);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Thank you, it is working now. Not sure why I never bothered to call the function I wrote. Not paying attention to the basics.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    See also: http://graphics.stanford.edu/~seander/bithacks.html("counting bits set") for some other neat ways to do this.

IMN logo majestic logo threadwatch logo seochat tools logo