Jump to content
  • Advertisement
Sign in to follow this  
JasonHise

when negation... isn't

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Yes - assuming a 32-bit int, test(0x80000000) returns true. This is because while -(2^31) can be expressed in a 2's complement 32-bit quantity, 2^31 cannot, so the negation overflows.

Share this post


Link to post
Share on other sites
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 Sneftel
Oh, and alternate solution:

#define -


Good god man, that's evil incarnate.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!