participate


New To Java - How do you Invert signed numbers?
<<   Back to Forum  |   Give us Feedback
This topic has 32 replies on 3 pages.    1 | 2 | 3 | Next »
richieboi22
Posts:5
Registered: 4/2/09
How do you Invert signed numbers?   
Aug 26, 2009 9:19 AM

 
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!
 
paul.miner
Posts:2,841
Registered: 10/8/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:22 AM (reply 1 of 32)  (In reply to original post )

 
n = -n
 
kevinaworkman
Posts:1,579
Registered: 30/03/09
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:27 AM (reply 2 of 32)  (In reply to original post )

 
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++;
   }
}
 
richieboi22
Posts:5
Registered: 4/2/09
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:27 AM (reply 3 of 32)  (In reply to #1 )

 
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.
 
richieboi22
Posts:5
Registered: 4/2/09
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:27 AM (reply 4 of 32)  (In reply to #2 )

 
No joke, I just tried it and it worked.
 
sabre150
Posts:22,045
Registered: 10/24/97
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:31 AM (reply 5 of 32)  (In reply to #1 )

 
paul.miner wrote:
n = -n

Not obscure enough! I prefer
int n = ....;
 n = (0xffffffff ^ n) + 1;
 
paul.miner
Posts:2,841
Registered: 10/8/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:45 AM (reply 6 of 32)  (In reply to #5 )

 
sabre150 wrote:
paul.miner wrote:
n = -n

Not obscure enough! I prefer
int n = ....;
 n = (0xffffffff ^ n) + 1;
Meh:
n = ~n + 1;
 
paul.miner
Posts:2,841
Registered: 10/8/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:49 AM (reply 7 of 32)  (In reply to #6 )

 
Or better:
n = ~--n;
 
sabre150
Posts:22,045
Registered: 10/24/97
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:49 AM (reply 8 of 32)  (In reply to #6 )

 
paul.miner wrote:
Meh:
n = ~n + 1;

:-)
 
JosAH
Posts:13,022
Registered: 4/6/04
Re: How do you Invert signed numbers?   
Aug 26, 2009 9:58 AM (reply 9 of 32)  (In reply to #5 )

 
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 ;-)
 
paul.miner
Posts:2,841
Registered: 10/8/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 10:11 AM (reply 10 of 32)  (In reply to #9 )

 
JosAH wrote:
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
Re: How do you Invert signed numbers?   
Aug 26, 2009 10:22 AM (reply 11 of 32)  (In reply to #10 )

 
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
 
jschell
Posts:38,148
Registered: 11/3/97
Re: How do you Invert signed numbers?   
Aug 26, 2009 11:46 AM (reply 12 of 32)  (In reply to #10 )

 
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.
 
paul.miner
Posts:2,841
Registered: 10/8/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 11:53 AM (reply 13 of 32)  (In reply to #12 )

 
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 :)
 
JEisen
Posts:299
Registered: 07/05/07
Re: How do you Invert signed numbers?   
Aug 26, 2009 12:20 PM (reply 14 of 32)  (In reply to original post )

 
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

Click to email this message Email this Topic

Edit this Topic
  
 
 
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.

Powered by Jive Forums