473,412 Members | 4,127 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,412 software developers and data experts.

INT_MIN as decimal

Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it, resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type [It appears to me
that the minus sign is not actually part of the literal, since it's not
part of a number token]
Mar 8 '06 #1
15 2470
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative?


Yes. In a 16bit classical two's-complement number, the values can range
from -32768 to 32767.

[snip]

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEDySNagVFX4UWr64RAkdZAJ4/S3hRCcOhmlQUnXDoxxJQpWBy8ACgy0zI
KVrkW7epRRwQvX2L/wtpQ8k=
=4i2Y
-----END PGP SIGNATURE-----
Mar 8 '06 #2
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it, resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type [It appears to me
that the minus sign is not actually part of the literal, since it's not
part of a number token]


In C99, 32768 will be of type long, so -32768 will have the expected
value.

Mar 8 '06 #3
Jordan Abel <ra*******@gmail.com> writes:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? [...]


Yes, but it has type "long int". If you want an "int" with the
same value, you can write -32767 - 1.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 8 '06 #4
Lew Pitcher <Le*********@tdsecurities.com> writes:
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative?


Yes. In a 16bit classical two's-complement number, the values
can range from -32768 to 32767.


True, but deceptive--see my article elsewhere in the thread.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Mar 8 '06 #5

"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it, resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type [It appears to me
that the minus sign is not actually part of the literal, since it's not
part of a number token]


As I read it, a decimal, rather than hex, constant that won't fit into an
int is a long. So it's a unary minus applied to a long, yielding a long.
Life is full of surprises today.

Different answer for 32-bit ints, if long is also 32 bits. Then it's long
long in C99. In C89, unary minus applied to 2^31 as unsigned long... er,
hmm, I see the problem

--
RSH
Mar 8 '06 #6
"Harald van DD3k" <tr*****@gmail.com> writes:
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? [...]


In C99, 32768 will be of type long, so -32768 will have the expected
value.


I don't think the situation is different in C90.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Mar 8 '06 #7
Ben Pfaff wrote:
"Harald van DD3k" <tr*****@gmail.com> writes:
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? [...]


In C99, 32768 will be of type long, so -32768 will have the expected
value.


I don't think the situation is different in C90.


I don't have C90 to check, but my compiler (GCC) does think it's
different, and it treats -2147483648 as positive unsigned in C90 mode,
and as negative signed in C99 mode.

Mar 8 '06 #8
"Harald van DD3k" <tr*****@gmail.com> writes:
Ben Pfaff wrote:
"Harald van DD3k" <tr*****@gmail.com> writes:
> Jordan Abel wrote:
>> Say int is a 16-bit twos-complement type.
>>
>> Is the expression (-32768) actually negative? [...]
>
> In C99, 32768 will be of type long, so -32768 will have the expected
> value.


I don't think the situation is different in C90.


I don't have C90 to check, but my compiler (GCC) does think it's
different, and it treats -2147483648 as positive unsigned in C90 mode,
and as negative signed in C99 mode.


In C90 mode, does it recognize long long integer constants? If
not, that would explain the discrepancy, I think.

The OP's situation is different, because `int' and `long' are
both in C90.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Mar 8 '06 #9
Ben Pfaff wrote:
"Harald van DD3k" <tr*****@gmail.com> writes:
Ben Pfaff wrote:
"Harald van DD3k" <tr*****@gmail.com> writes:

> Jordan Abel wrote:
>> Say int is a 16-bit twos-complement type.
>>
>> Is the expression (-32768) actually negative? [...]
>
> In C99, 32768 will be of type long, so -32768 will have the expected
> value.

I don't think the situation is different in C90.


I don't have C90 to check, but my compiler (GCC) does think it's
different, and it treats -2147483648 as positive unsigned in C90 mode,
and as negative signed in C99 mode.


In C90 mode, does it recognize long long integer constants? If
not, that would explain the discrepancy, I think.

The OP's situation is different, because `int' and `long' are
both in C90.


In C90 mode, it fully supports long long, constants and variables
(though with an optional warning). However, you are right. In C89,
according to the C99 rationale, 2147583648 is too large for int, too
large for long, not too large for unsigned long, so that is its type.
unsigned int is not considered, only unsigned long is. Thanks. I am not
entirely sure though, because a little bit later I read this:

QUIET CHANGE IN C89
Unsuffixed integer constants may have different types. In K&R,
unsuffixed decimal constants greater than INT_MAX, and unsuffixed octal
or hexadecimal constants greater than UINT_MAX are of type long.

So it basically says that in C89, unsuffixed decimal constants greater
than INT_MAX, such as 32768 in the original question, are not of type
long. And I can't think of any type other than unsigned int that would
be appropriate for it.

Mar 8 '06 #10
>Jordan Abel wrote:
Say int is a 16-bit twos-complement type. ...
Is the expression (-32768) actually negative?

In article <jw*******************@news20.bellglobal.com>,
Lew Pitcher <Le*********@tdsecurities.com> wrote:Yes. In a 16bit classical two's-complement number, the values can range
from -32768 to 32767.


That range is correct; but I think you missed Jordan Abel's point:
the token sequence you get in C source code is not:

-32768

i.e., one token, but rather:

-
32768

i.e., two separate tokens. The first token is the unary minus
operator. The second token is an integer constant, expressed in
decimal. The rules for typing integer constants say that we have
to look at the mathematical value, and see what type(s) can
hold them. If int uses the usual 16 bit two's-complement,
INT_MAX is 32767, so 32768 "means" the same thing as 32768U:
it has type "unsigned int". So we have, in effect:

-(32768U)

which, using 16-bit unsigned int arithmetic, is +32768.

Hence, if you have a <limits.h> file you can read, and you look at
it, you will find that INT_MIN is typically defined as:

#define INT_MIN (-32767 - 1)

or:

#define INT_MIN (-2147483647 - 1)

Without doing some kind of arithmetic, you get the wrong type!
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 8 '06 #11
In article <du********@news3.newsguy.com> I wrote, in part:
... If int uses the usual 16 bit two's-complement,
INT_MAX is 32767, so 32768 "means" the same thing as 32768U:
it has type "unsigned int".
This is wrong; it has type "long" (which goes to at least +217483647).
So we have, in effect:

-(32768U)
This should be -(32768L).
Hence, if you have a <limits.h> file you can read, and you look at
it, you will find that INT_MIN is typically defined as:

#define INT_MIN (-32767 - 1)

or:

#define INT_MIN (-2147483647 - 1)

Without doing some kind of arithmetic, you get the wrong type!


This is still right, though.

Also, C99 changes what -2147483648 means with 32-bit "int" since C99
has "long long".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 8 '06 #12
Ben Pfaff wrote:
Jordan Abel <ra*******@gmail.com> writes:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? [...]


Yes, but it has type "long int". If you want an "int" with the
same value, you can write -32767 - 1.


Or you can write "INT_MIN", which probably expands to your
expression after #include <limits.h>. You already know this, but
the reader may not.

--
"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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 8 '06 #13
Jordan Abel wrote:

Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it,
resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type [It appears to me
that the minus sign is not actually part of the literal,
since it's not
part of a number token]


The order is {int, long, long unsigned}
for choosing the type of an integer constant
according to value ranges.

--
pete
Mar 9 '06 #14
pete <pf*****@mindspring.com> writes:
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.

Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it,
resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type [It appears to me
that the minus sign is not actually part of the literal,
since it's not
part of a number token]


The order is {int, long, long unsigned}
for choosing the type of an integer constant
according to value ranges.


That's true for C90. In C99, the order is int, long int, long long
int. (That's for unsuffixed decimal constants.)

Since long long int is required to be at least 64 bits, presumably
it's not as important to allow a decimal literal to be treated as
unsigned; a programmer knows enough to write a correct 19-digit
decimal literal is going to know enough to use a suffix, or to use
hexadecimal or octal.

--
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.
Mar 9 '06 #15
Jordan Abel wrote:
Say int is a 16-bit twos-complement type.
Is the expression (-32768) actually negative?
Yes, but the expression has type long, not int.
Or is it the unsigned int literal 32768 with the unary minus operator
applied to it, resulting in the value of the expression being an
unsigned int 32768, which can then trap when being assigned to a
signed int type
It's a long literal with the unary minus operator applied.

Since -32768L _is_ within the range of your signed int, it won't trap
on
a corresponding C90 or C99 implementation.

Note that only C99 has the potential for implementation defined signals
on
assignment to a signed integer of a value not in range of that integer.
For
C90 the conversion is always implementation defined (i.e. unspecified
but
legal).

Had you written (-0x8000) the expression would yield a positive
unsigned
value and you might have a problem under C99.
[It appears to me that the minus sign is not actually
part of the literal, since it's not part of a number token]


Correct. All integer constants are non-negative. All floating point
constants are non-negative too. Adding an unary - (or unary +)
makes it a constant expression.

--
Peter

Mar 9 '06 #16

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

Similar topics

6
by: M Welinder | last post by:
The title more or less says it all: in C99, is the value of INT_MIN % -1 well defined (when performed as signed integers) under the assumption of two-complement representation. Note, that...
2
by: jzhang918 | last post by:
Hi, Should f(INT_MIN) return 0 according to the C99 for the following function f (), or its result is undefined? int f(int i) { i = i 0 ? i : -i; if (i<0) return 0;
41
by: p_cricket_guy | last post by:
Please see this test program: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <limits.h> 4 5 int main (void) 6 { 7 int y = -2147483648; 8 int x = INT_MIN;
30
by: viza | last post by:
Hi all int i= INT_MIN; unsigned int u= -i; Is u guaranteed to have the absolute value of INT_MIN? Why it might not: -i has type (int), and -INT_MIN might be more than INT_MAX.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.