Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
>> sign information) and has the same alignment requirements. 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." 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.
 
Reply With Quote
 
 
 
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
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>
Try the download section.



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

 
Reply With Quote
 
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/
 
Reply With Quote
 
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
 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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
 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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.

 
Reply With Quote
 
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function for direct conversion integer > slv aleksazr@gmail.com VHDL 2 12-25-2012 07:22 PM
where can I find good samples for efficient computation of matrix multiplication? walala VHDL 2 03-24-2010 10:06 AM
Direct Download Movies - No Download Limits - Download DivX DVDMovies hussain dandan Python 0 12-06-2009 04:52 AM
GPS : Basic pseudo-distance computation le Cl? VHDL 15 04-11-2009 09:22 AM
"Symbolic computation" Jesper Sahner Java 3 12-06-2004 10:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57