Thread: Checking bits in a float

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Checking bits in a float

    Hey,

    I'm wandering if there is an easy way to check the number of bits set to 1 in a binary number (4 byte ints). I know I can convert the int into an array of chars (or int for that matter) with each entry representing a bit, but that is time- consuming. I only need to know if there an odd or even numver of 1's in an int.

    Any ideas?

    Thanks.
    Everything is relative...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can also use a bitset if you want:

    Code:
    #include <iostream>
    #include <bitset>
    #include <limits>
    using namespace std;
    
    int main()
    {
        unsigned value = 12345;
        bitset<numeric_limits<unsigned>::digits> bits(value);
        
        cout << "Decimal: " << value
             << "\nBinary: " << bits
             << "\n# bits on: " << bits.count()
             << "\n# bits off: " << numeric_limits<unsigned>::digits - bits.count()
             << endl;
    
        return 0;
    }
    Output:
    Code:
    Decimal: 12345
    Binary: 00000000000000000011000000111001
    # bits on: 6
    # bits off: 26
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I can't open Dave's link from the comp I'm using, download limits, etc.

    I'l read up a bit more on bitset.
    Everything is relative...

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The link had this (among others).
    Code:
    int bits_set(unsigned int n)
    {
       n = (n & 0x55555555) + ((n & 0xaaaaaaaa) >> 1);
       n = (n & 0x33333333) + ((n & 0xcccccccc) >> 2);
       n = (n & 0x0f0f0f0f) + ((n & 0xf0f0f0f0) >> 4);
       n = (n & 0x00ff00ff) + ((n & 0xff00ff00) >> 8);
       n = (n & 0x0000ffff) + ((n & 0xffff0000) >> 16);
       return n;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Test one bit at a time with a bitwise AND.

    I'll assume that the title of your post is wrong. It's tricky with a float (which has a mantessa and an exponent)... easy with an int.

    You don't need to change the number to a char.

    You do need to check one bit at a time... You need a loop that runs 32 times... Count-up the number of "ones" that you find.

    You test each bit by performing a bitwise AND on each bit.

    X & 1 is true (non-zero) if X bit-zero is 1.
    X & 2 is true (non-zero) if X bit-1 is 1.
    X & 4 is true (non-zero) if X bit-2 is 1.
    X & 8 is true (non-zero) if X bit-3 is 1.
    etc...

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by DougDbug
    You need a loop that runs 32 times...
    You could loop fewer times, too. Perhaps once for every set bit.
    Code:
    int bits_set(unsigned int n)
    {
       int count = 0;
       while ( n )
       {
          n = n & (n - 1); /* clear lsb */
          ++count;
       }
       return count;
    }
    Last edited by Dave_Sinkula; 08-08-2005 at 12:41 PM. Reason: Added comment.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  2. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  3. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  4. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM