Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Odd Occurrences In Array  RSS feed

 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I'm not sure if I can give any more hints without just giving it away, so I might as well just post the answer:



XOR , how did you think about that ? i really want to learn
 
Saloon Keeper
Posts: 9120
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I've used XOR before to solve similar problems. Effective Java by Joshua Bloch also quotes a snipped of code that some C programmers would use to swap two variables without using a temporary variable:
This will not work in Java, because Java performs strict left-to-right evaluation. This however will:
 
Ranch Hand
Posts: 1154
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Because I've used XOR before to solve similar problems. Effective Java by Joshua Bloch also quotes a snipped of code that some C programmers would use to swap two variables without using a temporary variable:
This will not work in Java, because Java performs strict left-to-right evaluation. This however will:



How about...


A single test case worked for me, so it must be correct.

Slightly more elegant than my single-line version, but arguably better than the three-line solution:

 
Stephan van Hulst
Saloon Keeper
Posts: 9120
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's a nice parlor trick, but there's no reason to avoid writing the swap that uses a temporary variable, which most people will understand instantly.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I think it's a nice parlor trick.


It's not just a parlour trick; it actually has many applications - such as fault-tolerance (specifically RAID5), cryptography, and graphics (it was one of the early ways of creating a mouse pointer or "floating cursor" - don't know if it's still used for that now or not).

It was also the method old computers used to use to 'zero' memory (because x ^ x == 0). Again, I don't know if it's still used for that now or not.

The reason for its use in cryptography is that there is no "un-XOR" function - that is: given x ^ y, there is no way to determine x or y without guessing if you don't know either of them (and hence the need for passwords to be secret).

It's also blisteringly fast - usually operating in a single machine cycle.

I can't believe I didn't spot it myself (until Stephan's post a while back) because it was one of the first things I thought of; but I didn't follow it through logically. Just stupid I guess.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 9120
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant swapping two variables without using a temporary variable, not the use of XOR in general
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!