473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Range of short int?

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 '05 #1
12 18733
al******************@hotmail.com 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

Dec 24 '05 #2
<al******************@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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
Dec 24 '05 #3
al******************@hotmail.com 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
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 24 '05 #4
Robert Gamble wrote:
al******************@hotmail.com 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

Dec 24 '05 #5
al******************@hotmail.com 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) ks***@mib.org <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.
Dec 24 '05 #6

<al******************@hotmail.com> 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).
Dec 24 '05 #7
Robert Gamble wrote:
al******************@hotmail.com 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.
Dec 24 '05 #8
"Malcolm" <re*******@btinternet.com> 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) ks***@mib.org <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.
Dec 24 '05 #9
al******************@hotmail.com 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
Dec 26 '05 #10
Keith Thompson <ks***@mib.org> writes:
"Malcolm" <re*******@btinternet.com> 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?
Dec 28 '05 #11
Tim Rentsch wrote:
Keith Thompson <ks***@mib.org> writes:

"Malcolm" <re*******@btinternet.com> 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
es*****@acm-dot-org.invalid
Dec 28 '05 #12
Tim Rentsch <tx*@alumnus.caltech.edu> 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 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dennis D. | last post by:
VB.net does not seem to have adequate structure for handling time within it's own code. Subtract 15 minutes from 00:00 AM, and an out of range condition results. Subtract 15 minutes from 12:00 PM,...
3
by: manning_news | last post by:
Using A2K. I've been asked to modify a report currently requiring only one date parameter to now accept a date range. The main report has 2 subreports and is not bound to a table or query. The...
16
by: Joona I Palaste | last post by:
Reading Skybuck Flying's obvious troll message, I thought of how to properly do a for loop that would iterate over the complete range of an unsigned variable. As you all know, Skybuck's method...
19
by: Harshan | last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!) I came to know that it uses 2's compliment...
2
by: sixdeuce62 | last post by:
Hello, I am trying to create a query that will prompt me to enter the parameter value if beginning date and ending date. I have created everything I need in the query, but I have to manually go...
3
by: Matt Brown - identify | last post by:
Hello, I'm trying to figure out a method to look up by a range of dates, entries in a database. The format of the date in the database is "M\D \yyyy HH:MM:SS". What i need to do is take the...
20
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
3
by: Deano | last post by:
The short version; In short, given one date range (start and end dates) how can I find the period that overlaps with another date range? The long version; I have knocked up a little application...
5
by: pereges | last post by:
I wrote a small program to check the range for int(signed) and long int(signed) on my machine: #include <stdio.h> #include <limits.h> int main(void) { printf("INT_MIN:%d INT_MAX: %d",...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.