Thread: How many bits?

  1. #1
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31

    How many bits?

    I have written a little program to find out the amount of bits in an unsigned int. By some reason the program prints nothing. It just goes into an infinite loop. What is wrong?

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main( void )
    {
        int bits = 1;
        unsigned int i = 2;
    
        while ( i < UINT_MAX )
        {
    	i *= 2;
    	bits++;
        }
    
        printf(" %d\n", bits );
    
        return 0;
    }
    Last edited by char; 05-18-2012 at 06:57 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    UINT_MAX is an odd number and i cannot be bigger -> your condition is never false.
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Or to put it another way, when i is ((unsigned long)UINT_MAX+1UL)/2UL, what is i*2?
    Hint: I think it is 0.

    Edit: Or to be pedantic, I think it is 0U.
    Last edited by pheininger; 05-18-2012 at 07:29 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    In any case, look into the macro CHAR_BITS and the sizeof() operator.

  5. #5
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Thank you guys!

    This was actually very obvious.

    Code:
    i < UINT_MAX
    will always be true since UINT_MAX is the upper limit for unsigned int. Declaring i as unsigned long fixes the problem.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Declaring i as unsigned long fixes the problem.
    That is, until you run the program on a machine where unsigned int and unsigned long are the same thing.
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    16
    You can also do it by AND-ing the integer with 1, 2, 4, 8, 16, ...
    Each AND will check the presence of one bit. Record the highest bit that was set.

    ___________
    Visit my project: Derivative Calculator

  8. #8
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Quote Originally Posted by TomasRiker View Post
    You can also do it by AND-ing the integer with 1, 2, 4, 8, 16, ...
    Each AND will check the presence of one bit. Record the highest bit that was set.

    ___________
    Visit my project: Derivative Calculator
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        unsigned int i = 2;
        unsigned int mask = 2;
        int counter = 1;
    
        while ( i & mask )
        {
    	i *= 2;
    	mask *= 2;
    	counter++;
        }
    
        printf( "%d\n", counter );
    
        return 0;
    }
    Is that what you mean? Surprisingly it returns 32, which is the right answer. I was expecting this loop to be infinite, since both i and mask have same offset and they are increased the same way.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I'd write it much simpler than that by starting i at 1 and counting how many times it can be multiplied by 2 (or shifted left one bit):
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long i = 1;
        int counter = 0;
    
        while (i) {
            i *= 2; // or i <<= 1;
            ++counter;
        }
    
        printf("%d\n", counter);
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    maybe not as exciting, but why not just


    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("%d\n", sizeof(unsigned long long int)*8);
    
        return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Yeah, well, there's always the boring way.

    But the 8 should be replaced with CHAR_BIT, in case a char is more than 8 bits.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Wouldn't it have made more sense to start at UINT_MAX and right shift 1 bit at a time until you get zero?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Quote Originally Posted by christop View Post
    I'd write it much simpler than that by starting i at 1 and counting how many times it can be multiplied by 2 (or shifted left one bit):
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long i = 1;
        int counter = 0;
    
        while (i) {
            i *= 2; // or i <<= 1;
            ++counter;
        }
    
        printf("%d\n", counter);
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        unsigned int i = 1;
        int counter = 0;
    
        while (i)
        {
            i <<= 1;
            counter++;
        }
    
        printf("%d\n", counter);
    
        return 0;
    }
    This one worked fine, as long as I declare i as unsigned int.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main( void )
    {
        unsigned int i = UINT_MAX;
        int counter = 1;
    
        while ( i >>= 1 )
            counter++;
    
        printf( "%d\n", counter );
    
        return 0;
    }
    was probably the most elegant one. And most portable maybe?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  2. 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
  3. 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
  4. bits like int x:4
    By esler in forum C Programming
    Replies: 5
    Last Post: 05-09-2002, 08:02 AM
  5. the stl: bits
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 07:49 PM