Help counting number of bits set in an integer

This is a discussion on Help counting number of bits set in an integer within the C Programming forums, part of the General Programming Boards category; I have to write a program that counts the number of bits set in an integer. For example, the number ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Code:
    while(i)
    {
       counter += i & 0x1;
       i >> 1;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 01:10 PM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    little typo vart
    Thanks
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21