1 2 3 Previous Next 32 Replies Latest reply on Sep 3, 2009 10:36 PM by 782681

# How do you Invert signed numbers?

Hi there.

A simple question: How can I invert the sign of an integer? ie) convert -12 to +12? No doubt there is a really simple way to do this, but I'm stumped, I cannot find anything and I've been working on this program all day.

I'll be very grateful to anyone who can let me know if Java provides a way to do this, or point me in the right direction if it takes some tweaking of binary numbers using the Math package or something.

Cheers!
• ###### 1. Re: How do you Invert signed numbers?
``n = -n``
• ###### 2. Re: How do you Invert signed numbers?
Is this a joke? If not, try this:
``````int x = numberToInvertSign;
boolean pos = x > 0;
for(int i = 0; i < 2*Math.abs(x); i++){
if(pos){
numberToInvertSign--;
}
else{
numberToInvertSign++;
}
}``````
• ###### 3. Re: How do you Invert signed numbers?
I knew it would be simple, but not that simple! I feel like a right dumbass now.

Thanks very much paul.miner, you saved the day.
• ###### 4. Re: How do you Invert signed numbers?
No joke, I just tried it and it worked.
• ###### 5. Re: How do you Invert signed numbers?
paul.miner wrote:
``n = -n``
Not obscure enough! I prefer
`````` int n = ....;
n = (0xffffffff ^ n) + 1;``````
• ###### 6. Re: How do you Invert signed numbers?
sabre150 wrote:
paul.miner wrote:
``n = -n``
Not obscure enough! I prefer
``````int n = ....;
n = (0xffffffff ^ n) + 1;``````
Meh:
``n = ~n + 1;``
• ###### 7. Re: How do you Invert signed numbers?
Or better:
``n = ~--n;``
• ###### 8. Re: How do you Invert signed numbers?
paul.miner wrote:
Meh:
``n = ~n + 1;``
:-)
• ###### 9. Re: How do you Invert signed numbers?
sabre150 wrote:
paul.miner wrote:
``n = -n``
Not obscure enough! I prefer
``````int n = ....;
n = (0xffffffff ^ n) + 1;``````
Bah, that +1 operation is way too clear; better make that:
``````n^= 0xffffffff;
int m;
for (m= 1; m != 0 && ((n&m) != 0); m<<= 1);
n|= m;
if (m == 0) n= m;
else for (m >>= 1; m != 0; n^= m, m>>=1);``````
kind regards,

Jos ;-)
• ###### 10. Re: How do you Invert signed numbers?
>
``else for (m >>= 1; m != 0; n^= m, m>>=1);``
Shouldn't this be an unsigned shift, at least for the initial shift?
``else for (m >>>= 1; m != 0; n^= m, m>>=1);``
• ###### 11. Re: How do you Invert signed numbers?
paul.miner wrote:
Shouldn't this be an unsigned shift, at least for the initial shift?
``else for (m >>>= 1; m != 0; n^= m, m>>=1);``
Yep, you are correct; I now go spank myself thank you ;-)

kind regards,

Jos
• ###### 12. Re: How do you Invert signed numbers?
paul.miner wrote:
Shouldn't this be an unsigned shift, at least for the initial shift?
else for (m >>>= 1; m != 0; n^= m, m>>=1);
Thank goodness we now have the correct form to use in all our code.
• ###### 13. Re: How do you Invert signed numbers?
jschell wrote:
paul.miner wrote:
Shouldn't this be an unsigned shift, at least for the initial shift?
else for (m >>>= 1; m != 0; n^= m, m>>=1);
Thank goodness we now have the correct form to use in all our code.
Especially since it only affects negating Integer.MIN_VALUE, the odd man out :)
• ###### 14. Re: How do you Invert signed numbers?
You guys are all trying way too hard. Clearly, you can do it just with regular addition and subtraction:
``````public int invert(int i) {
return i - (i + i);
}``````
1 2 3 Previous Next