Data Types

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1848 Views - Last Post: 27 May 2012 - 09:12 AM

#16 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Data Types

Posted 15 May 2012 - 02:55 AM

Much simpler that that. Try using the XOR operator:
x = x XOR y
y = x XOR y
x = x XOR y



Now, they are swapped. XOR in Java is ^
Was This Post Helpful? 0
  • +
  • -

#17 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Data Types

Posted 27 May 2012 - 09:12 AM

View PostDogstopper, on 11 May 2012 - 08:22 AM, said:

Here's a good one in addition to yours:

How can you swap two positive integers without using a third variable.

int1 += int2 - (int2 = int1);


That should do it. :)

EDIT: I probably should have read the other posts before posting. >.>

This post has been edited by Gungnir: 27 May 2012 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2