What am I doing wrong.

Discussion in 'Mac Programming' started by seriypshick, Oct 6, 2005.

  1. seriypshick macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #1
    I want to calculate the maximun number that 4 byte integer can hold. However it doesn't seem o work(Gives me '0'). What am I dong wrong?
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int count, max_size, total;
    	
    	max_size = 4 * 8;	/* Calculate how many bits 4 bytes have */
    	total = 1;
    	count = 1;
    	printf("%i \n", total);
    
    	while(count <= max_size)
    	{
    		total *= 2;
    		++count;
    	}
    	printf("%i \n", total);
    	
    	return 0;
    }
     
  2. Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    An signed integer cannot hold a positive 32 bit value, i.e. 2^32, even an unsigned integer will just show you the values from 0 to 2^32-1.
     
  3. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #3
    Ok, i know that, it doesn't matter.
    My question is why it gives me 0? What am I doing wrong?

    Edit: never mind i got it now.
     
  4. Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #4
    Did you get it to work with a an unsigned long int? I didn't, I had to go with a long long int to make it work:

    Code:
    #include <stdio.h>
    
    int main()
    {
            int count, max_size; 
            long long int total;
            
            max_size = 32;       /* Calculate how many bits 4 bytes have */
            total = 1;
            count = 1;
            printf("%lli \n", total);
    
            while(count <= max_size)
            {
                    total *= 2;
                    ++count;
            }
            printf("%lli \n", total);
            
            return 0;
    }
    
     
  5. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #5
    If you're curious, there are simpler ways of finding what you want. Here's a couple of one line examples:

    Code:
    long long highestValue = ((long long)1 << 4 * 8) - 1;
    This line uses the bitshift operator (<<), which translates roughly as "times two to the power of". In reality, it is shifting bits to the left by the specified number, eg 19 (00010011 in binary) would become 38 (00100110) if you had the code "19 << 1".

    It casts 1 as a long long before the bitshift operation, because otherwise it is treated as an int which is not large enough to store such a high value.

    Code:
    unsigned int highestValue = -1;
    Firstly, this code assumes that an unsigned int is 4 bytes, so is obviously platform dependent. It also takes advantage of the wrap around property of integers. Since an unsigned int cannot store a value of -1, and -1 is one less than the minimum value it can store, the variable is instead set to the highest value it can store (-2 would give the second highest, and so on). Although simple, it is a bit of a hack and will produce a compiler warning in some compilers.

    To print these numbers, you'd need to use different format specifiers to "%i" as it is only for 4 byte ints. For long longs, use "%qi", and for unsigned ints, use "%u".

    The reason it doesn't work with a long is that both ints and longs are the same size in Mac OS X (32 bits).
     
  6. Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #6
    As the long should be, of course (that the int is just as long is a more dodgy issue). 2^32 = 4294967296 and the unsigned long int covers 0 - 4294967295. Silly me. :eek:
     
  7. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #7
    I used double.

    How big is long long. 8 bytes (64 bit)?

    Also is there a way to show what this would be: (binary 32 bits):
    Code:
    11111111111111111111111111111111
    Is this what I have to do to get it.
    Code:
    	long long highestValue = ((long long)32 << 4 * 8) - 1;
    Thanks.
     
  8. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #8
    Although doubles can store values much larger than an int, they are floating-point numbers so are generally only used with non-integers. For most scenarios, it is better to use types that store integers only, as they are more accurate (eg you'd never have a variable storing 1.999999999 when it should be storing 2).

    Yes, so it can store numbers over 4 billion times as large as an int.

    Either of the code snippets I gave in my previous post will give that number, or if you subtract 1 from the (fixed) code from the original post it will also give it. Your code above would return an answer of 32 * 2 ^32 - 1 (which simplifies to 2^37 - 1), which is not what you want. In general, you could use this code:
    Code:
    	long long highestValue = ((long long)1 << [the number of bits]) - 1;
     
  9. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #9
    Thanks
     

Share This Page