[SOLVED]Problem with signedness

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

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 on other sites
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 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 on other sites
Oh, and if you really want to see how to do it you might want to visit this page.

Share on other sites
so..just to be clear.. I do ~(x+1) and thats it??

Share on other sites
Quote:
 Original post by moussen15so..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 on other sites
Quote:
 Original post by moussen15so..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 on other sites
Quote:
 Original post by DrSpockIn twos complement form, a negative value is denoted by 1 in the signed ( most significant ) bit:10000001 = -100000001 = +1

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

Enigma

Share on other sites
Right, sorry heh. Somehow i got mixed up typing that out.

Share on other sites
Quote:
 Original post by moussen15so..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)

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×