Range of short int?

Discussion in 'C Programming' started by alistair_happencross@hotmail.com, Dec 24, 2005.

1. Guest

In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
1999 reprint), this is on pages 71-72:

"we think of a short int as an object that can take on values between
-32768 and 32767, instead of as a 16-bit object"

My understanding of C is that the negative number should be -32767. The
errata list on his website shows that it was -32767 and was changed to
-32768 for subsequent printings.

Which is correct?

, Dec 24, 2005

2. Robert GambleGuest

wrote:
> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
> 1999 reprint), this is on pages 71-72:
>
> "we think of a short int as an object that can take on values between
> -32768 and 32767, instead of as a 16-bit object"
>
> My understanding of C is that the negative number should be -32767. The
> errata list on his website shows that it was -32767 and was changed to
> -32768 for subsequent printings.
>
> Which is correct?

The most negative value for a signed short int is at least -32767, on a
2's complement machine this will be -32768.

Robert Gamble

Robert Gamble, Dec 24, 2005

3. William J. Leary Jr.Guest

<> wrote in message
news:...
> My understanding of C is that the negative number should be -32767. The
> errata list on his website shows that it was -32767 and was changed to
> -32768 for subsequent printings.

0x8001 = -32767
0x8000 = -32768

I remember this from years ago, but as 2's complement issue, not a C one.

At that time it was being referred to (at least in the circles I occupied) as
the "negative zero" issue. I never saw the problem myself, but it caused a big
flap amongst the guys doing the microcode for the math unit. I seem to recall
even understanding why it was an issue to them, though I've forgotten from this
distance in time.

- Bill

William J. Leary Jr., Dec 24, 2005
4. Chuck F.Guest

wrote:
>
> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition,
> January 1999 reprint), this is on pages 71-72:
>
> "we think of a short int as an object that can take on values
> between -32768 and 32767, instead of as a 16-bit object"
>
> My understanding of C is that the negative number should be
> -32767. The errata list on his website shows that it was -32767
> and was changed to -32768 for subsequent printings.
>
> Which is correct?

Neither. It depends on how the system was designed. The actual
values, for your system, are described in <limits.h>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Chuck F., Dec 24, 2005
5. Robert GambleGuest

Robert Gamble wrote:
> wrote:
> > In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
> > 1999 reprint), this is on pages 71-72:
> >
> > "we think of a short int as an object that can take on values between
> > -32768 and 32767, instead of as a 16-bit object"
> >
> > My understanding of C is that the negative number should be -32767. The
> > errata list on his website shows that it was -32767 and was changed to
> > -32768 for subsequent printings.
> >
> > Which is correct?

>
> The most negative value for a signed short int is at least -32767, on a
> 2's complement machine this will be -32768.

On a 2's complement machine *using 16 bits*, that will be -32768, the
magnitude could be larger if more bits are used in the representation.

Robert Gamble

Robert Gamble, Dec 24, 2005
6. Keith ThompsonGuest

writes:
> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
> 1999 reprint), this is on pages 71-72:
>
> "we think of a short int as an object that can take on values between
> -32768 and 32767, instead of as a 16-bit object"
>
> My understanding of C is that the negative number should be -32767. The
> errata list on his website shows that it was -32767 and was changed to
> -32768 for subsequent printings.

The minimum guaranteed range of short is -32767 .. +32767. On a
two's-complement system (i.e., almost all modern systems), a 16-bit
signed type is capable of representing an additional negative value,
-32768. For absolute portability, you shouldn't assume that short can
represent -32768.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Dec 24, 2005
7. MalcolmGuest

<> wrote
> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
> 1999 reprint), this is on pages 71-72:
>
> "we think of a short int as an object that can take on values between
> -32768 and 32767, instead of as a 16-bit object"
>
> My understanding of C is that the negative number should be -32767. The
> errata list on his website shows that it was -32767 and was changed to
> -32768 for subsequent printings.
>
> Which is correct?
>

The first.
If you want to treat a short as an abstract integer, rather than a series of
bits, the obvious reason is so that you are not tied to two's complement
representation.
Therefore you need to follow the standard, which guarantees the range -32767
to + 32767 only. (-32768 might be used as a trap representation, for
example, or the machine might be one's complement, or use a weird and
wonderful system not yet devised).

Malcolm, Dec 24, 2005
8. Flash GordonGuest

Robert Gamble wrote:
> wrote:
>> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
>> 1999 reprint), this is on pages 71-72:
>>
>> "we think of a short int as an object that can take on values between
>> -32768 and 32767, instead of as a 16-bit object"
>>
>> My understanding of C is that the negative number should be -32767. The
>> errata list on his website shows that it was -32767 and was changed to
>> -32768 for subsequent printings.
>>
>> Which is correct?

>
> The most negative value for a signed short int is at least -32767, on a
> 2's complement machine this will be -32768.

According to N1124 for a 2s complement machine sign bit set and all
other bits 0 is allowed to be a trap representation, so it could still
be -32767. Admittedly I'm not aware of any systems that do this.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Flash Gordon, Dec 24, 2005
9. Keith ThompsonGuest

"Malcolm" <> writes:
[...]
> If you want to treat a short as an abstract integer, rather than a series of
> bits, the obvious reason is so that you are not tied to two's complement
> representation.
> Therefore you need to follow the standard, which guarantees the range -32767
> to + 32767 only. (-32768 might be used as a trap representation, for
> example, or the machine might be one's complement, or use a weird and
> wonderful system not yet devised).

C99 6.2.6.2 limits the possibilites to two's complement, ones'
complement, and sign and magnitude. (Yes, the placement of the
apostrophes is correct.)

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Dec 24, 2005
10. Emmanuel DelahayeGuest

a écrit :
> In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January
> 1999 reprint), this is on pages 71-72:
>
> "we think of a short int as an object that can take on values between
> -32768 and 32767, instead of as a 16-bit object"
>
> My understanding of C is that the negative number should be -32767. The
> errata list on his website shows that it was -32767 and was changed to
> -32768 for subsequent printings.
>
> Which is correct?

-32767 is correct from the C language definition point of view. It is
the guaranteed minimum value for a [short ]int.

-32768 could be a value of INT_MIN or SHRT_MIN on a platform with
negatives coded with 2-complement. It's a plateform question.

--
A+

Emmanuel Delahaye

Emmanuel Delahaye, Dec 26, 2005
11. Tim RentschGuest

Keith Thompson <> writes:

> "Malcolm" <> writes:
> [...]
> > If you want to treat a short as an abstract integer, rather than a series of
> > bits, the obvious reason is so that you are not tied to two's complement
> > representation.
> > Therefore you need to follow the standard, which guarantees the range -32767
> > to + 32767 only. (-32768 might be used as a trap representation, for
> > example, or the machine might be one's complement, or use a weird and
> > wonderful system not yet devised).

>
> C99 6.2.6.2 limits the possibilites to two's complement, ones'
> complement, and sign and magnitude. (Yes, the placement of the
> apostrophes is correct.)

Just curious - do you have any idea why the writing of (only) one of
these terms changed between C99 and n1124? Surely it would be better
if the two terms were written consistently; anyone have any idea why
they aren't?

Tim Rentsch, Dec 28, 2005
12. Eric SosmanGuest

Tim Rentsch wrote:

> Keith Thompson <> writes:
>
>
>>"Malcolm" <> writes:
>>[...]
>>
>>>If you want to treat a short as an abstract integer, rather than a series of
>>>bits, the obvious reason is so that you are not tied to two's complement
>>>representation.
>>>Therefore you need to follow the standard, which guarantees the range -32767
>>>to + 32767 only. (-32768 might be used as a trap representation, for
>>>example, or the machine might be one's complement, or use a weird and
>>>wonderful system not yet devised).

>>
>>C99 6.2.6.2 limits the possibilites to two's complement, ones'
>>complement, and sign and magnitude. (Yes, the placement of the
>>apostrophes is correct.)

>
>
> Just curious - do you have any idea why the writing of (only) one of
> these terms changed between C99 and n1124? Surely it would be better
> if the two terms were written consistently; anyone have any idea why
> they aren't?

Detail-oriented readers and copy editors should notice the
position of the apostrophe in terms like "two's complement" and
"ones' complement": A two's complement number is complemented
with respect to a single power of 2, while a ones' complement
number is complemented with respect to a long sequence of 1s.
Indeed, there is also a "twos' complement notation," which has
radix 3 and complementation with respect to (2...22)_3.

-- D.E. Knuth, "The Art of Computer Programming, Volume II:
Seminumerical Algorithms" (third edition) section 4.5.

--
Eric Sosman
lid

Eric Sosman, Dec 28, 2005
13. Guest

Tim Rentsch <> wrote [re. ones' and two's complement]
>
> Just curious - do you have any idea why the writing of (only) one of
> these terms changed between C99 and n1124? Surely it would be better
> if the two terms were written consistently; anyone have any idea why
> they aren't?

<http://en.wikipedia.org/wiki/Talk:Signed_number_representations>

-Larry Jones

I'll be a hulking, surly teen-ager before you know it!! -- Calvin

, Jan 1, 2006