Thread: Help counting number of bits set in an integer

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Help counting number of bits set in an integer

    I have to write a program that counts the number of bits set in an integer. For example, the number 5 (decimal), which is 0000000000000101 (binary), has two bits set. I know how to count the number of bits in a char, but I don't know how to count the extra 8 bits in an integer. How would i navigate to the other byte and count the eight bits?

    Here is how i'd count the number of bits in a char...

    Code:
    #include <stdio.h>
    
    void main()
    {
            char ch = 5;   /*assign it a random number*/
            int counter = 0;
            int i;
            
            for (i = 0x80; i > 0; i = (i >> 1))  {
                 if ((ch & i) != 0)
                    ++counter;
            }
    
    
    }
    Thanks for any help with this...

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while(i)
    {
       counter += i & 0x1;
       i >> 1;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns an int

    > How would i navigate to the other byte and count the eight bits?
    Other 8 ?
    The size of an int varies from machine to machine, as do all the other types.

    If you include limits.h, then the number of bits in an int is
    CHAR_BIT * sizeof(int)

    > for (i = 0x80;
    Well the easy thing to do is start with a different constant which starts with the most significant bit of the int.

    Hint
    unsigned int allOnes = ~0;
    Using that and some bit operations, you should be able to find the value of the most significant bit in an int without explicitly saying it in your code.
    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. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
     i >> 1;
    Code:
    i >>= 1;
    little typo vart
    Last edited by sl4nted; 12-07-2006 at 02:10 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    little typo vart
    Thanks
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Right shift on signed numbers is implementation-defined behaviour.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 01:30 PM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Counting integer digits
    By Lionmane in forum C Programming
    Replies: 22
    Last Post: 05-24-2005, 10:11 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM