Velocity Reviews > Direct computation of integer limits in K&R2?

# Direct computation of integer limits in K&R2?

Ioannis Vranos
Guest
Posts: n/a

 03-13-2008
Ioannis Vranos wrote:
> Richard Heathfield wrote:
>> Peter Nilsson said:
>>
>>> Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr> wrote:
>>>> #include <stdio.h>
>>>>
>>>> int main()
>>>> {
>>>> unsigned x= -1;
>>>> int INTMAX=x /2;
>>> What if UINT_MAX == INT_MAX,

>>
>> I don't think it can. "For each of the signed integer types, there is
>> a corresponding (but different) unsigned integer type (designated with
>> the keyword unsigned) that uses the same amount of storage (including
>> 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." So for UINT_MAX to be ==
>> INT_MAX, ints would need to squeeze twice as many values into the same
>> number of bits as unsigned ints.
>>
>>> or UINT_MAX = 4*INT_MAX+3?

>>
>> See above.
>>
>>>> int INTMIN= -INTMAX -1;
>>> What if INT_MIN == -INT_MAX?

>>
>> That, however, is a valid objection. For example, INT_MIN might be
>> -32767 rather than -32768.

>
>
>
> C95:
>
> 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.

Richard Heathfield
Guest
Posts: n/a

 03-13-2008
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@
"Usenet is a strange place" - dmr 29 July 1999

CBFalconer
Guest
Posts: n/a

 03-13-2008
Ben Bacarisse wrote:
> Kaz Kylheku <> writes:
>

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

Totally unnecessary. All those integral max values are specified
in <limits.h>. That's why.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dik T. Winter
Guest
Posts: n/a

 03-13-2008
In article <> Flash Gordon <> writes:
> Micah Cowan wrote, On 12/03/08 05:55:
> > writes:
> >
> >> On Mar 11, 7:30 pm, Micah Cowan <mi...@cowan.name> wrote:
> >>> Flash Gordon <s...@flash-gordon.me.uk> writes:

>
> <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/

pete
Guest
Posts: n/a

 03-13-2008
Richard Heathfield wrote:
>
> Peter Nilsson said:
>
> > Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr> wrote:
> >> #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

Richard Heathfield
Guest
Posts: n/a

 03-13-2008
pete said:

> Richard Heathfield wrote:
>>
>> Peter Nilsson said:
>>
>> > Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr> wrote:
>> >> #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@
"Usenet is a strange place" - dmr 29 July 1999

pete
Guest
Posts: n/a

 03-13-2008
Richard Heathfield wrote:
>
> pete said:
>
> > Richard Heathfield wrote:
> >>
> >> Peter Nilsson said:
> >>
> >> > Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr> wrote:
> >> >> #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

Richard Heathfield
Guest
Posts: n/a

 03-13-2008
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@
"Usenet is a strange place" - dmr 29 July 1999

Ioannis Vranos
Guest
Posts: n/a

 03-13-2008
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.

Ioannis Vranos
Guest
Posts: n/a

 03-13-2008
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?