473,802 Members | 1,940 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 3804
In article <8f************ @news.flash-gordon.me.ukFla sh Gordon <sp**@flash-gordon.me.ukwri tes:
Micah Cowan wrote, On 12/03/08 05:55:
mu*****@gmail.c om writes:
On Mar 11, 7:30 pm, Micah Cowan <mi...@cowan.na mewrote:
Flash Gordon <s...@flash-gordon.me.ukwri tes:

<snip trap representation in 2s complement>
Huh. I managed to forget that somehow. My bad, Flash.

It's easy to forget. I'm not actually aware of any implementations which
make use of this freedom.
(Sign bit 1 other bits 0 is a trap representation. ) Some Gould machines
did it, or were it the Modcomps? I disremember and do not have the manuals
here, but one of the two.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 13 '08 #31
Richard Heathfield wrote:
>
Peter Nilsson said:
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,

I don't think it can.
It can.

sizeof(int) == 2
sizeof(unsigned ) == 2
CHAR_BIT == 16
INT_MAX == 0xffff
UINT_MAX == 0xffff

--
pete
Mar 13 '08 #32
pete said:
Richard Heathfield wrote:
>>
Peter Nilsson said:
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,

I don't think it can.

It can.

sizeof(int) == 2
sizeof(unsigned ) == 2
CHAR_BIT == 16
INT_MAX == 0xffff
UINT_MAX == 0xffff
"The range of nonnegative values of a signed integer type is a subrange of
the corresponding unsigned integer type, and the representation of the
same value in each type is the same."

Since, in your example, int has 16 value bits, and since there must also be
a sign bit, that makes 17 bits altogether that contribute to the value. I
could be wrong, of course, but doesn't that mean that unsigned int must
also have 17 bits that contribute to the value?

--
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 #33
Richard Heathfield wrote:
>
pete said:
Richard Heathfield wrote:
>
Peter Nilsson said:

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,

I don't think it can.
It can.

sizeof(int) == 2
sizeof(unsigned ) == 2
CHAR_BIT == 16
INT_MAX == 0xffff
UINT_MAX == 0xffff

"The range of nonnegative values
of a signed integer type is a subrange of
the corresponding unsigned integer type,
and the representation of the
same value in each type is the same."
That's exactly what I've shown.
Since, in your example, int has 16 value bits,
and since there must also be a sign bit,
that makes 17 bits altogether that contribute to the value. I
could be wrong, of course,
but doesn't that mean that unsigned int must
also have 17 bits that contribute to the value?
You're mixing terms.

"value bits" != "bits that contribute to the value"

N869
6.2.6.2 Integer types
[#2] For signed integer types, the bits of the object
representation shall be divided into three groups: value
bits, padding bits, and the sign bit. There need not be any
padding bits; there shall be exactly one sign bit. Each bit
that is a value bit shall have the same value as the same
bit in the object representation of the corresponding
unsigned type (if there are M value bits in the signed type
and N in the unsigned type, then M<=N).
Your claim implies that you believe that M can't equal N.

--
pete
Mar 13 '08 #34
pete said:

<snip>
(if there are M value bits in the signed type
and N in the unsigned type, then M<=N).
Your claim implies that you believe that M can't equal N.
I sit corrected. Thank you.

--
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 #35
pete wrote:
>
You're mixing terms.

"value bits" != "bits that contribute to the value"

N869
6.2.6.2 Integer types
[#2] For signed integer types, the bits of the object
representation shall be divided into three groups: value
bits, padding bits, and the sign bit. There need not be any
padding bits; there shall be exactly one sign bit. Each bit
that is a value bit shall have the same value as the same
bit in the object representation of the corresponding
unsigned type (if there are M value bits in the signed type
and N in the unsigned type, then M<=N).
Your claim implies that you believe that M can't equal N.

What is N869? My answer as C95 based. Actually since it is an exercise
of K&R2, it is a C90 question.

Mar 13 '08 #36
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.

Can you mention the chapter of K&R2 where this exercise is?

Mar 13 '08 #37
On Mar 13, 1:53 pm, Ioannis Vranos <ivra...@nospam .no.spamfreemai l.gr>
wrote:
pete wrote:
You're mixing terms.
"value bits" != "bits that contribute to the value"
N869
6.2.6.2 Integer types
[#2] For signed integer types, the bits of the object
representation shall be divided into three groups: value
bits, padding bits, and the sign bit. There need not be any
padding bits; there shall be exactly one sign bit. Each bit
that is a value bit shall have the same value as the same
bit in the object representation of the corresponding
unsigned type (if there are M value bits in the signed type
and N in the unsigned type, then M<=N).
Your claim implies that you believe that M can't equal N.

What is N869? My answer as C95 based. Actually since it is an exercise
of K&R2, it is a C90 question.
Does C90 guarantee absence of padding bits? I didn't find
anything like that (though I didn't try hard because this
question is about C standard history, not about pathologies
permitted by the C standard).

The quoted text is from C99, N869 is a C99 draft, and
the C90 text (quoted by Richard) is still true in C99.

Yevgen
Mar 13 '08 #38
On Mar 13, 12:24*pm, Ioannis Vranos
<ivra...@nospam .no.spamfreemai l.grwrote:
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.

Can you mention the chapter of K&R2 where this exercise is?
Exercise 2.1, page 36
Mar 13 '08 #39
ym******@gmail. com wrote:

<snip>
Does C90 guarantee absence of padding bits? I didn't find
anything like that (though I didn't try hard because this
question is about C standard history, not about pathologies
permitted by the C standard).
C90 doesn't seem to mention padding bits. But is that equivalent to not
allowing them?
The quoted text is from C99, N869 is a C99 draft, and
the C90 text (quoted by Richard) is still true in C99.
Yes. A draft of C89 is available in the clc-wiki site, formerly hosted
by Dan Pop.

Mar 13 '08 #40

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.