473,789 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Direct computation of integer limits in K&R2?

Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

Mar 11 '08
88 3800
On Mar 11, 2:37*pm, santosh <santosh....@gm ail.comwrote:
Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.
You can use shifting to determine how many bits there are in the given
signed integral type. Start with 1 and keep shifting it left until it
drops off. With that information, you can construct the greatest
possible positive integer value: which is all 1's except for the sign
bit, which is zero. The greatest possible negative value is either the
additive inverse of that value, or, in the case of two's complement,
that value less one. You can detect whether two's complement is in
effect by applying a simple test to the value -1:

switch (-1 & 3) {
case 1: /* ...01: sign magnitude */
break;
case 2: /* ...10: one's complement */
break;
case 3: /* ...11: two's complement */
break;
}

That's the general approach I'd take to the exercise.


Mar 12 '08 #21
On Mar 12, 2:35 pm, Kaz Kylheku <kkylh...@gmail .comwrote:
On Mar 11, 2:37 pm, santosh <santosh....@gm ail.comwrote:
Hello all,
In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

You can use shifting to determine how many bits there are in the given
signed integral type. Start with 1 and keep shifting it left until it
drops off.
That's UB, no?
With that information, you can construct the greatest
possible positive integer value: which is all 1's except for the sign
bit, which is zero. The greatest possible negative value is either the
additive inverse of that value, or, in the case of two's complement,
that value less one.
And this may be a trap representation.

Yevgen
Mar 12 '08 #22

"santosh" <sa*********@gm ail.comwrote in message
news:fr******** **@registered.m otzarella.org.. .
Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.
I don't think there's a perfect answer.

However this is the closest I could get.

double x = 0;
int testme;

do
{
x++;
testme = (int) x;
} while((double) testme == x);

printf("Biggest integer %g\n", x - 1);

It will fail if all ints are not exactly representable by a double. Which is
an int 64 machine.
(Wail, gnash).

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 12 '08 #23
On Mar 12, 1:23*pm, ymunt...@gmail. com wrote:
On Mar 12, 2:35 pm, Kaz Kylheku <kkylh...@gmail .comwrote:
You can use shifting to determine how many bits there are in the given
signed integral type. Start with 1 and keep shifting it left until it
drops off.

That's UB, no?
Unfortunately it is. Shifting a bit into the sign is UB. Only a
positive value whose double is representable may be shifted left by
one bit.

This means that the sign bit is quite impervious to bit manipulation.

Mar 12 '08 #24
"Malcolm McLean" <re*******@btin ternet.comwrite s:
"santosh" <sa*********@gm ail.comwrote in message
news:fr******** **@registered.m otzarella.org.. .
>Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.
I don't think there's a perfect answer.

However this is the closest I could get.

double x = 0;
int testme;

do
{
x++;
testme = (int) x;
} while((double) testme == x);

printf("Biggest integer %g\n", x - 1);
I don't think you need to be so cautious -- ints must use binary, so
you could start at 1 and repeatedly double x and try to convert x-1.
Even so, you have not gained anything -- the conversion to int, when
it is out of range, is still undefined.

--
Ben.
Mar 12 '08 #25
Kaz Kylheku <kk******@gmail .comwrites:
On Mar 12, 1:23Â*pm, ymunt...@gmail. com wrote:
>On Mar 12, 2:35 pm, Kaz Kylheku <kkylh...@gmail .comwrote:
You can use shifting to determine how many bits there are in the given
signed integral type. Start with 1 and keep shifting it left until it
drops off.

That's UB, no?

Unfortunately it is. Shifting a bit into the sign is UB. Only a
positive value whose double is representable may be shifted left by
one bit.

This means that the sign bit is quite impervious to bit
manipulation.
It must participate in other bit operations, though, like ~, &, | and
^. Even so,I can't see any way to avoid UB when trying to calculate
the range of int. Equally, I don't have a persuasive argument that it
*can't* be done, either.

--
Ben.
Mar 13 '08 #26
On Mar 12, 5:30*pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Kaz Kylheku <kkylh...@gmail .comwrites:
On Mar 12, 1:23*pm, ymunt...@gmail. com wrote:
On Mar 12, 2:35 pm, Kaz Kylheku <kkylh...@gmail .comwrote:
You can use shifting to determine how many bits there are in the given
signed integral type. Start with 1 and keep shifting it left until it
drops off.
That's UB, no?
Unfortunately it is. Shifting a bit into the sign is UB. Only a
positive value whose double is representable may be shifted left by
one bit.
This means that the sign bit is quite impervious to bit
manipulation.

It must participate in other bit operations, though, like ~, &, | and
^. *Even so,I can't see any way to avoid UB when trying to calculate
the range of int. *Equally, I don't have a persuasive argument that it
*can't* be done, either.
To compound things, imagine a C implementation where all integral
types were 64 bits (including char).
Even the undefined behavior hacks I posted will fail on those.
In short, I think it is a really difficult problem to solve.
If someone can define a sensible solution, I would be very pleased to
see it.
It might be interesting to see what DMR has to say about it.
Mar 13 '08 #27
santosh wrote:
Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

C95:

#include <stdio.h>
int main()
{
unsigned x= -1;

int INTMAX=x /2;

int INTMIN= -INTMAX -1;

printf("INTMIN: %d\t", INTMIN);

printf("INTMAX: %d\n", INTMAX);

return 0;
}
Mar 13 '08 #28
Ioannis Vranos <ivra...@nospam .no.spamfreemai l.grwrote:
#include <stdio.h>

int main()
{
* * *unsigned x= -1;
* * *int INTMAX=x /2;
What if UINT_MAX == INT_MAX, or UINT_MAX = 4*INT_MAX+3?
* * *int INTMIN= -INTMAX -1;
What if INT_MIN == -INT_MAX?
* * *printf("INTMIN : %d\t", INTMIN);
* * *printf("INTMAX : %d\n", INTMAX);
* * *return 0;
}
--
Peter
Mar 13 '08 #29
Ioannis Vranos said:

<snip>
>Since sizeof(N)= sizeof(signed N)= sizeof(unsigned N)

where N can be char, short, int, long

and as you mentioned they use the same amount of storage, how can
INT_MIN be equal to -INT_MAX since the range of values is the same.
Sign-and-magnitude representation.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 13 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.