Thread: Help with programming with bits

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    Help with programming with bits

    THe problem statement asks "Given that the expression ~0 produces an integer that contains all 1s, write a functioncalled int_size that returns the number of bits contained in an int on your
    particular machine."

    The solution is:
    Code:
    int  int_size (void)
    {
        unsigned int  bits;
        int           size = 0;
    
        bits = ~0;
    
        while ( bits ) {
           ++size;
           bits >>= 1;
        }
    
        return size;
    }

    Is the while loop saying while (bits=1)?
    If so, I don't understand why bits has to equal one for the size to increase.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    while ( bits )
    could be written
    while ( bits > 0 )

    bits >>= 1;
    You can unfold that into this:
    bits = bits >> 1;
    Shifting right once will drop a bit so you count it. Eventually you get a size etc.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Yes, I think I understand that, but why does bits have to be >0?
    A bit can be 0 or 1. So why is it exclusively 1 in this case?
    Is it because before the while loop bits is set equal to ~0?

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    #include <limits.h>
    ...
    #define INT_BITS (CHAR_BIT * sizeof (int))

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That seems to be a mistake - what should have been stated was "while ( bits != 0 )" because in 'C,' non-zero is considered true and zero is considered false.

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    It's not really a mistake since it's an unsigned int.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the reason you say bits > 0 in the loop condition is so that you count all of the bits. Eventually all of the bits will be turned off, you get zero, which means you are done counting.

    The reason that bit >>= 1 is used is because the loop is supposed to go like this:
    1111
    0111
    0011
    0001
    0000

    IOW, you only need to shift one bit at a time.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Barney McGrew View Post
    It's not really a mistake since it's an unsigned int.
    You're right, I stand corrected.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    Well the reason you say bits > 0 in the loop condition is so that you count all of the bits. Eventually all of the bits will be turned off, you get zero, which means you are done counting.
    Does this have anything to do with bits=~0?

    What if you had bits=101. Wouldn't this yield size=1, since the while loops well terminate once it hits the "0" in "101."

  10. #10
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    If the binary representation of an integer is 10, what is its decimal value?

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by Barney McGrew View Post
    If the binary representation of an integer is 10, what is its decimal value?
    0*2^0+1*2^1=2

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    10 in binary is still more than 0.

  13. #13
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Right. So, until the value of bits reaches 0, the value stored by size will continue to be incremented.

    edit: Think of it this way:

    101 (5) > 0? Yes.
    10 (2) > 0? Yes.
    1 (1) > 0? Yes.
    0 (0) > 0 ? No.

    Which would terminate with 3 stored in size.
    Last edited by Barney McGrew; 01-15-2013 at 11:57 PM.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Bitwise operations happen on a bit-by-bit level. A '~' is just an inverse.

    Code:
     Bits: 101
    ~Bits: 010
    This speaks nothing of size.

    In your example, to count the bits, you'd set them all to one and count the ones. What's a quick way to set all bits to one? Set them all to zero and invert them all (to one).

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by Barney McGrew View Post
    Right. So, until the value of bits reaches 0, the value stored by size will continue to be incremented.

    edit: Think of it this way:

    101 (5) > 0? Yes.
    10 (2) > 0? Yes.
    1 (1) > 0? Yes.
    0 (0) > 0 ? No.

    Which would terminate with 3 stored in size.
    Thank you guys. THis explanation really helped.
    I forgot in the while argument that #>0, where # is represented in decimal and not binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How many bits?
    By char in forum C Programming
    Replies: 12
    Last Post: 05-19-2012, 03:46 AM
  2. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  3. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  4. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM
  5. Help Please...bits
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-24-2002, 01:43 PM