Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple question on inverting values  RSS feed

 
klisie koo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i guess this should be a very simple question:
int x=55;
int y=11;
how can i invert these 2 values without using extra variable?
thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no easy way... i seem to recall there being a way to do it, but it's really not worth it. it's confusing, complicated, hard to understand, and unnecesary.
why do you want to do this?
 
klisie koo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it?? i heard that it could be done in less than 4 lines..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well... maybe "easy" isn't the right word. there's no way (that i know of) that isn't confusing to read.
it's much more obvious what you're doing if you DO use a temp variable, so why not just use it? This sounds like a homework problem, and if so, nobody here is going to want to just give it to you.
If you have some reason for doing it, you'll be hard pressed to convice me its a valid reason, but i'd like to hear it.
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?
x = x+y;
y = x-y;
x = x-y;
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it could be done in one line only, yes ONE LINE ONLY. However there is a small problem... it will work on C/C++ compilers only, it is an operator preceding thing i guess.
Here it is

You can try what Ramesh suggested, it will work fine with integer values however you won't get accurate results when dealing with doubles and floats (Check the code below). The safest way is to use a third 'temp' variable as fred suggested

[ January 12, 2004: Message edited by: Vicken Karaoghlanian ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Note - this post isn't really beginner level, but it's a natual followup to what's gone before. If you're confused, just ignore this post and the first part of Vicken's post, and use a temp variable as Vicken later suggests. Anything else isn't worth the hassle.)
[Vicken]: Actually it could be done in one line only, yes ONE LINE ONLY. However there is a small problem... it will work on C/C++ compilers only, it is an operator preceding thing i guess.
These work:
a = (b = (a = b ^ a) ^ b) ^ a;
or
a = (b ^= (a ^= b)) ^ a;
The key is that the last time ^ is executed, it needs the latest value of a as one of its arguments. If we write
a ^= b^= a ^= b;
that's equivalent to
a ^= (b ^= (a ^= b));
or
a = a ^ (b = b ^ (a = a ^ b));
and the problem is that when that bold a is evaluated, it uses the original value of a - rather than the value after the other operations have been evaluated. Java always evaluates left-hand operands before right-hand operands, even if it can't perform the operation itself until after both left and right sides have been evaluated. So the solution is to rearrange the order so that bold a is after everything else:
a = (b = b ^ (a = a ^ b)) ^ a;
Which has many equivalent forms, including the two I showed initially.
[ January 12, 2004: Message edited by: Jim Yingst ]
 
klisie koo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all your prompt responses
yes, i agree it's probably not worth all the hassle, so all the more i appreciate your replies
Ramesh's suggestion would be closer to what i want, thanks!
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim... well use of the brackets, it never crossed my mind. After all it could be done in one line only and the good part is that it also can be done in Java too.
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But exactly because of this:
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." -- Martin Fowler
I think the
x = x+y;
y = x-y;
x = x-y;
answer might have been a better solution.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Elouise Kivineva:
But exactly because of this:
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." -- Martin Fowler
I think the
x = x+y;
y = x-y;
x = x-y;
answer might have been a better solution.

I was showing klisie what options she had, i didn't say it was the best way to do it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!