Sign in to follow this  
JasonHise

when negation... isn't

Recommended Posts

It's an artifact of the 2's complement representation used in the microprocessor, not so much C/C++ itself. I don't know of anything in particular in the C++ standard that would prohibit non-2's complement architectures, but maybe there's something.

Try it yourself. See what's returned from test(MIN_INT).

It's because (say for 8-bit integers) they can range from -128 to +127. If you try and take the negation of -128, you can't because +128 is not representable. Binary-wise, you get -128 again.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by etothex
It's an artifact of the 2's complement representation used in the microprocessor, not so much C/C++ itself. I don't know of anything in particular in the C++ standard that would prohibit non-2's complement architectures, but maybe there's something.


C's standard requires sign-magnitude, 1's complement, or 2's complement representation for signed integers. I would guess a similar requirement is in C++. I think only sign-magnitude avoids this problem.

Share this post


Link to post
Share on other sites
1's complement does it okay, too; the only representation with the problem is 2's complement, because the positive and negative ranges don't match.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by etothex
It's an artifact of the 2's complement representation used in the microprocessor, not so much C/C++ itself. I don't know of anything in particular in the C++ standard that would prohibit non-2's complement architectures, but maybe there's something.


C's standard requires sign-magnitude, 1's complement, or 2's complement representation for signed integers. I would guess a similar requirement is in C++. I think only sign-magnitude avoids this problem.

Not explicitly. Although there are some other requirements that might make certain alternative representations illegal.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by daerid
Quote:
Original post by Sneftel
Oh, and alternate solution:

#define -


Good god man, that's evil incarnate.

Oh...heh. I thought his post got cut short before the actual solution. That is clever.

Thinking about it...I could do that in much of my code and still have it compile. Things like --i and i -= 5 won't cause syntax errors on their own [although the latter might lead to a missing assignment operator], and how often do you subtract, really?

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by daerid
Quote:
Original post by Sneftel
Oh, and alternate solution:

#define -

Good god man, that's evil incarnate.

Except for the little detail that it won't get past the preprocessor in any standard C or C++ compiler, yeah, that is evil incarnate. [wink]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Conner McCloud
Quote:
Original post by Anonymous Poster
C's standard requires sign-magnitude, 1's complement, or 2's complement representation for signed integers. I would guess a similar requirement is in C++. I think only sign-magnitude avoids this problem.

Not explicitly. Although there are some other requirements that might make certain alternative representations illegal.


Well I'l be... I would've sworn it explicitly said so, but all I can find is that the value of the sign bit must simply negate the value, have value -2^N, or have value 1-2^N.

Share this post


Link to post
Share on other sites
Quote:
C++ Standard, Section 3.9.1, Paragraph 7
Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral types.43) A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system.44) [Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. ]

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by MrAccident
Quote:
Original post by daerid
Quote:
Original post by Sneftel
Oh, and alternate solution:

#define -

Good god man, that's evil incarnate.

Except for the little detail that it won't get past the preprocessor in any standard C or C++ compiler, yeah, that is evil incarnate. [wink]


You're right, VC++ 2005 gives me a "#define syntax error" for that (and any variation of it).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this