Is there a way to calculate the maximum value representable by unsigned int
without using limits.h
(so no UINT_MAX
) or without using
unsigned int z = 0;
z = z - 1;
Is there a way to calculate the maximum value representable by unsigned int
without using limits.h
(so no UINT_MAX
) or without using
unsigned int z = 0;
z = z - 1;
The simplest way to do this is to simply assign -1 to an unsigned int
. You could also assign ~0u
to it.
If that's not acceptable, while inefficient, you could do something like this:
unsigned int i = 0;
while (i+1 > 0)
i++;
printf("i=%u\n", i);
Your z = z - 1
determines the value at run-time arithmetically. It can be determined as a compile time constant without implicit or explicit casts:
unsigned int z = ~0u ;
That will be what your tutor is looking for if he has any credibility.
Otherwise if you really must provide an "algorithim" to get a grade in a clearly flawed assignment, then;
unsigned z = 1 ;
for( unsigned b = 1;
b != 0;
z = (z << 1) | 1, b <<= 1 ) ;
printf( "z = %u\n", z ) ;
That is a somewhat terse way of writing:
unsigned z = 1 ; // Initial set LSB of max-int to 1
for( unsigned b = 1; // "Walk" a 1 through each bit of an unsigned
b != 0; // until the 1 falls of the end
b <<= 1 ) // move the 1 right
{
z = (z << 1) | 1 ; // shift max-int right and set LSB to 1
}
Another alternative:
unsigned z = 1 ;
unsigned p = 0 ;
while( z != p )
{
z = (z << 1) | 1 ;
p = (p << 1) | 1 ;
}
In this solution p
has one fewer bits than z
until both are "all ones" when they are equal.
The only possible advantage of these loop methods is that by adding a counter you can simultaneously determine the maximum value and the bit width:
unsigned z = 1 ;
unsigned bits = 0 ;
for( unsigned b = 1;
b != 0;
z = (z << 1) | 1, b <<= 1, bits++ ) ;
printf( "z = %u %u-bits\n", z, bits ) ;
(unsigned int)(-1)
or static_cast<unsigned int>(-1)
are guaranteed to give the maximum value representable by unsigned int
. These are compile-time constants.
I am guessing that this is an academic question based on not knowing the underlying architecture of the computer that a program will run on. E.g. in the 80's we had 8 bit computers, then it moved to 16 bit, then 32 and now 64 bit. (think there may be 128 bit).
The formula below calculates the largest value
int max = pow(2, number of bits assigned to data type) — 1;
e.g. for 8 bits you get 2^8 - 1 = 255
To get the number of bytes available for the unsigned int type use sizeof. sizeof is part of the c core language so does not need limits.h
So that is quite easy, C and C++ has the function sizeof
. This will return the number of bytes a variable of this type needs. So for example on my system
sizeof(int)
will return 4. So 4 bytes are needed to store a int
value.
Now to calculate the highest number that can be fit in this variable you first need to make sure if the variable is signed
or unsigned
.
Why? A signed variable (meaning it also can store negative values) uses the first bit of its memory to indicate if it is positive or negative.
As the name suggest, a unsigned int
is unsigned
. So your calculation will look like this:
2^(sizeof(unsigned int) * 8) - 1
.
Why? sizeof
return the number of bytes the variable needs, but this calculation only works with bits. Because 1 Byte = 8 Bit
you have to multiply by 8.
Why the - 1? Your computer starts counting with 0 not with 1.
If you want to calculate the highest number storable in a signed variable you calculate like this:
2^(sizeof(int) * 8 - 1) - 1
.
But this is only half, your signed variable stores from -some amount to +some amount
right? So 2^(sizeof(int) * 8 - 1) - 1
is your upper bound and -2^(sizeof(int) * 8 - 1) + 1
is your lower bound.