473,789 Members | 2,514 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 Sat, 15 Mar 2008 02:39:40 -0000, yalong <ag*******@gmai l.comwrote:
Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel

And,there is a way about signed char on that book
printf("signed char min = %d\n", -(char)((unsigne d char) ~0 >1));

but i think it should be that
printf("signed char min = %d\n", -1 -(char)((unsigne d char) ~0 >1));
maybe it's a little mistake of press :)
I think you've misread the symbol ~ as -. In another message in this
thread I posted this, an extract of T&G's solution:

printf("signed char min = %d\n", ~(char)((unsign ed char) ~0 >1));
printf("signed char max = %d\n", (char)((unsigne d char) ~0 >1));
printf("unsigne d short min = %d\n", ~(short)((unsig ned short) ~0 >1));
printf("unsigne d short max = %d\n", (short)((unsign ed short) ~0 >1));

--
Martin

Mar 16 '08 #81
On Mar 17, 6:19 am, Martin <martindotunder scoreobr...@whi ch.tnet>
wrote:
On Sat, 15 Mar 2008 02:39:40 -0000, yalong <aginob...@gmai l.comwrote:
Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel
And,there is a way about signed char on that book
printf("signed char min = %d\n", -(char)((unsigne d char) ~0 >1));
but i think it should be that
printf("signed char min = %d\n", -1 -(char)((unsigne d char) ~0 >1));
maybe it's a little mistake of press :)

I think you've misread the symbol ~ as -. In another message in this
thread I posted this, an extract of T&G's solution:

printf("signed char min = %d\n", ~(char)((unsign ed char) ~0 >1));
printf("signed char max = %d\n", (char)((unsigne d char) ~0 >1));
printf("unsigne d short min = %d\n", ~(short)((unsig ned short) ~0 >1));
printf("unsigne d short max = %d\n", (short)((unsign ed short) ~0 >1));

--
Martin
hi Martin,

Thanks about that. I got a misprint book of my language vision.
Both of them get the right value, maybe it's little different in
running time.

yalong
Mar 17 '08 #82
yalong <aginob...@gmai l.comwrote:
santosh <santosh....@gm ail.comwrote:He llo 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.

Hi santosh

Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel
Or maybe you shouldn't.
And,there is a way about signed char on that book
printf("signed char min = %d\n",
-(char)((unsigne d char) ~0 >1));
~0 may be a trap representation on sign-magnitude machines.
The (unsigned char) ~0 is much better stated as (unsigned
char) -1, which yields UCHAR_MAX portably. But the single
right shifting of that need not yield SCHAR_MAX.[*] The
further negation of that need not yield SCHAR_MIN.
[*] The standard doesn't preclude UCHAR_MAX == 65535 and
SCHAR_MAX == 127.
but i think it should be that
printf("signed char min = %d\n",
-1 -(char)((unsigne d char) ~0 >1));
Excellent. All the flaws above and one more. ;)
maybe it's a little mistake of press :)
The cast conversion to _plain_ char is certainly a mistake,
even if inconsequencial in context. But I doubt it's a
printing error.

--
Peter
Mar 17 '08 #83
On Mar 16, 10:37*pm, Peter Nilsson <ai...@acay.com .auwrote:
yalong <aginob...@gmai l.comwrote:
santosh <santosh....@gm ail.comwrote:He llo 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.
Hi santosh
Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel

Or maybe you shouldn't.
And,there is a way about signed char on that book
printf("signed char min = %d\n",
*-(char)((unsigne d char) ~0 >1));

~0 may be a trap representation on sign-magnitude machines.
The (unsigned char) ~0 is much better stated as (unsigned
char) -1, which yields UCHAR_MAX portably. But the single
right shifting of that need not yield SCHAR_MAX.[*] The
further negation of that need not yield SCHAR_MIN.
[*] The standard doesn't preclude UCHAR_MAX == 65535 and
SCHAR_MAX == 127.
but i think it should be that
printf("signed char min = %d\n",
* -1 -(char)((unsigne d char) ~0 >1));

Excellent. All the flaws above and one more. ;)
maybe it's a little mistake of press :)

The cast conversion to _plain_ char is certainly a mistake,
even if inconsequencial in context. But I doubt it's a
printing error.

--
Peter
This seems rather simple to me, but perhaps it is just my
inexperience. Here is a solution that works on all of the platforms
I've worked with, but I haven't worked with ALL computers:
unsigned long long ULL_MAX = (unsigned long long)(-1LL);
unsigned long long ULL_MIN = 0ULL; //automatically known because the
smallest unsigned value is 0
signed long long SLL_MAX = (signed long long)(ULL_MAX >1);
signed long long SLL_MIN = SLL_MAX + 1;

It is also somewhat easily adaptable. Just change the types from long
long to another integral type!
Mar 17 '08 #84
rpgfan3233 said:

<snip>
This seems rather simple to me, but perhaps it is just my
inexperience. Here is a solution that works on all of the platforms
I've worked with, but I haven't worked with ALL computers:
unsigned long long ULL_MAX = (unsigned long long)(-1LL);
unsigned long long ULL_MIN = 0ULL; //automatically known because the
smallest unsigned value is 0
signed long long SLL_MAX = (signed long long)(ULL_MAX >1);
signed long long SLL_MIN = SLL_MAX + 1;
Presumably you mean SLL_MIN = -SLL_MAX - 1;

Unfortunately, this last one can break on sign-and-magnitude
implementations . Worse, as we were reminded quite recently, C doesn't
forbid implementations from setting U<int_type>_MA X equal to
<int_type>_MA X.

--
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 17 '08 #85
CBFalconer <cb********@yah oo.comwrites:
Ioannis Vranos wrote:
>>
... snip ...
>>
OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?

Yes. Consider a 16 bit system. INT_MAX is 32767. INT_MIN is
-32768 (or -32767). UINT_MAX is 65536 (UINT_MIN is naturally 0).
Or it would be 0 if it existed.
It is allowable to reserve the value that would represent -32768 to
indicate a special condition, such as uninitialized. Or, for a 1's
complement machine, the bit pattern for -0 is forbidden.
How does this answer the question? You've demonstrated that it's
allowed, which he already knows. The question is whether they're
different on any existing system. (The really interestion question,
IMHO, is "if so, which one?")

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 17 '08 #86
Peter Nilsson <ai***@acay.com .auwrites:
yalong <aginob...@gmai l.comwrote:
<snip>
>And,there is a way about signed char on that book
printf("sign ed char min = %d\n",
-(char)((unsigne d char) ~0 >1));

~0 may be a trap representation on sign-magnitude machines.
Did you mean ones' complement? I thought sign bit = 1 and all value
bits 1 had to be well-defined on sign-magnitude systems.

On ones' complement systems, ~0 is either negative zero (and the
conversion to unsigned char will produce 0) or it is a trap
representation. The above code is definitely useless on such a
system.

--
Ben.
Mar 17 '08 #87
On Mar 18, 4:08*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Peter Nilsson <ai...@acay.com .auwrites:
yalong <aginob...@gmai l.comwrote:
<snip>
And,there is a way about signed char on that book
printf("signed char min = %d\n",
*-(char)((unsigne d char) ~0 >1));
~0 may be a trap representation on sign-magnitude machines.

Did you mean ones' complement?
Yes, thanks.

--
Peter
Mar 17 '08 #88
CBFalconer wrote:
Ioannis Vranos wrote:
... snip ...
>OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?

Yes. Consider a 16 bit system. INT_MAX is 32767. INT_MIN is
-32768 (or -32767). UINT_MAX is 65536 (UINT_MIN is naturally 0).
It is allowable to reserve the value that would represent -32768 to
indicate a special condition, such as uninitialized. Or, for a 1's
complement machine, the bit pattern for -0 is forbidden.
Off by one.. 65536 takes 17 bits. Happy St. Paddy's.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Mar 17 '08 #89

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.