Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
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;
}
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,442
1,093
Bergen, Norway
seriypshick said:
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?
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.
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
Mitthrawnuruodo said:
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.
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.
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,442
1,093
Bergen, Norway
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;
}
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
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".

Mitthrawnuruodo said:
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:

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).
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,442
1,093
Bergen, Norway
HexMonkey said:
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).
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:
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
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.
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
seriypshick said:
I used double.
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).

seriypshick said:
How big is long long. 8 bytes (64 bit)?
Yes, so it can store numbers over 4 billion times as large as an int.

seriypshick said:
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;
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;
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
HexMonkey said:
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;

Thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.