By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

-x == ~x + 1

P: n/a
Will -x == ~x + 1 always be true if x is an unsigned integer type?
This certainly won't always hold if x is signed. The reason I come to
this conclusion is that unsigned numbers must wrap, and there is no
special logic for handling negative numbers. This would then cause
unsigned numbers to behave as 2-s complement numbers, and thus
guarantee -x == ~x + 1.
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 6 Dec 2003 17:04:28 -0800, in comp.lang.c , c_********@yahoo.com
(Craig) wrote:
Will -x == ~x + 1 always be true if x is an unsigned integer type?


Yes AFAIK.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #2

P: n/a
"Craig" <c_********@yahoo.com> wrote in message
news:15**************************@posting.google.c om...
Will -x == ~x + 1 always be true if x is an unsigned integer type?
For unsigned types not subject to integral promotion to int, yes.
This certainly won't always hold if x is signed.


The expression potentially invokes undefined behaviour for signed types.

--
Peter
Nov 13 '05 #3

P: n/a
In article <15**************************@posting.google.com >,
c_********@yahoo.com (Craig) wrote:
Will -x == ~x + 1 always be true if x is an unsigned integer type?
Yes, as long as that unsigned interger type will not be promoted to
int. For example, if the type is "unsigned char" or "unsigned short" (or
"char" if "char" is an unsigned type on your platform), then the result
is undefined.

But, if the type is "unsigned int", "unsigned long" or "unsigned long
long", then the result will be true.
This certainly won't always hold if x is signed.


Correct.

Nov 13 '05 #4

P: n/a
Clark Cox wrote:
In article <15**************************@posting.google.com >,
c_********@yahoo.com (Craig) wrote:

Will -x == ~x + 1 always be true if x is an unsigned integer type?

Yes, as long as that unsigned interger type will not be promoted to
int. For example, if the type is "unsigned char" or "unsigned short" (or
"char" if "char" is an unsigned type on your platform), then the result
is undefined.


What if the type is unsigned char, unsigned short, or char (being
unsigned on this theoretical platform), but the type does not promote to
int?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #5

P: n/a
In article <Df*****************@newsread1.news.pas.earthlink. net>,
Kevin Goodsell <us*********************@neverbox.com> wrote:
Clark Cox wrote:
In article <15**************************@posting.google.com >,
c_********@yahoo.com (Craig) wrote:

Will -x == ~x + 1 always be true if x is an unsigned integer type?

Yes, as long as that unsigned integer type will not be promoted to
int. For example, if the type is "unsigned char" or "unsigned short" (or
"char" if "char" is an unsigned type on your platform), then the result
is undefined.


What if the type is unsigned char, unsigned short, or char (being
unsigned on this theoretical platform), but the type does not promote to
int?

-Kevin


Well, as per my first sentence ...
Yes, as long as that unsigned integer type will not be promoted to

int.

...it will not be undefined. (Though, looking my post now, I admit I
could have been a little clearer).

Nov 13 '05 #6

P: n/a
In <Df*****************@newsread1.news.pas.earthlink. net> Kevin Goodsell <us*********************@neverbox.com> writes:
Clark Cox wrote:
In article <15**************************@posting.google.com >,
c_********@yahoo.com (Craig) wrote:
Will -x == ~x + 1 always be true if x is an unsigned integer type?


Yes, as long as that unsigned interger type will not be promoted to
int. For example, if the type is "unsigned char" or "unsigned short" (or
"char" if "char" is an unsigned type on your platform), then the result
is undefined.


What if the type is unsigned char, unsigned short, or char (being
unsigned on this theoretical platform), but the type does not promote to
int?


Then, it promotes to unsigned int and there are no problems. The
previous poster was quite clear that only the promotion to int breaks it.

OTOH, (with the exception of unsigned short getting "promoted" to unsigned
int) your scenario is not going to happen on a hosted implementation.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.