This topic has
32
replies
on
3
pages.
1
|
2
|
3
|
Next »
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!
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++;
}
}
I knew it would be simple, but not that simple! I feel like a right ******* now.
Thanks very much paul.miner, you saved the day.
No joke, I just tried it and it worked.
sabre150
Posts:22,045
Registered: 10/24/97
Not obscure enough! I prefer
int n = ....;
n = (0xffffffff ^ n) + 1;
Not obscure enough! I prefer
int n = ....;
n = (0xffffffff ^ n) + 1;
Meh:
n = ~n + 1;
sabre150
Posts:22,045
Registered: 10/24/97
JosAH
Posts:13,022
Registered: 4/6/04
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 ;-)
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);
JosAH
Posts:13,022
Registered: 4/6/04
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
jschell
Posts:38,148
Registered: 11/3/97
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.
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 :)
JEisen
Posts:299
Registered: 07/05/07
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);
}
This topic has
32
replies
on
3
pages.
1
|
2
|
3
|
Next »
Back to Forum
Read the Developer Forums Code of Conduct
Email this Topic
Edit this Topic
Site Upgrade
Forums 7.1.8 was deployed Oct 26th. The release consists of minor fixes & a few enhancements.
Forums Statistics
Users Online : 28 Guests : 129
About Sun forums
Oracle Forums is a large collection of user generated
discussions. It is here to help you ask questions, find answers, and
participate in discussions.
Check out our guide on Getting
started with Oracle Forums for a full walkthrough of how to best
leverage the benefits of this community.