Jump to content
  • Advertisement
Sign in to follow this  
moussen15

[SOLVED]Problem with signedness

This topic is 4836 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

Please help me understand this. I've made a macro for converting negative numbers to positive and leave positive ones intact: #define ABSOLUTE(x) (x & (~(1 << (sizeof(x)*8-1)))) that should leave all bits except the signed flag as they were right? this is how I think it works: 1 << sizeof(x)*8 -1 = 10000000 NOTing that: 01111111 ANDing that with three 10000011 should give 00000011 but when I try it doesn't work. Thanks in advance [EDIT] Thank you all for your help! Rating++

Share this post


Link to post
Share on other sites
Advertisement
Your computer probably uses 2's complement representations for its negative numbers, not sign-magnitude like you seem to assume. In order to reverse the sign of a two's complement number you need to add one and then flip all the bits.

Share this post


Link to post
Share on other sites
It won't work because modern computers use slightly more complicated two's complement system, it would've worked on one's complement systems though.

edit: a minute too late..

Share this post


Link to post
Share on other sites
Quote:
Original post by moussen15
so..just to be clear.. I do ~(x+1) and thats it??
Actually, I think that's what you'd use to make a positive value negative, not the other way around. And you'd have to use a special case for negative values too.
To make a negative value positive you'd use ~(x-1) or ~x-1 instead, the code below uses the latter.

Here's a purely arithmetic implementation that (should) work:
signed negate(signed value) {
signed tmp;

tmp = value >> (sizeof(value) * CHAR_BIT - 1);
value ^= tmp;
value -= tmp;

return value;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by moussen15
so..just to be clear.. I do ~(x+1) and thats it??


you would have to check the signed bit first, if it is 1, then you need to ~(x+1) otherwise, its already positive. In twos complement form, a negative value is denoted by 1 in the signed ( most significant ) bit:

11111111 = -1
00000001 = +1

[Edited by - DrSpock on August 17, 2005 5:40:40 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by DrSpock
In twos complement form, a negative value is denoted by 1 in the signed ( most significant ) bit:

10000001 = -1
00000001 = +1

That would be ones complement. In twos complement:
10000001 = -127
00000001 = +1
-1 = (~00000001) + 1 = 11111111

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by moussen15
so..just to be clear.. I do ~(x+1) and thats it??


You should invert then add 1, works on both types without checking.

#define negate(x) x = (~x + 1)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!