3

How to swap two variables’ values without a temporary variable. I failed :/.

Comments
  • 4
    OK, I'll try.

    I assume both variables are numbers, say a and b

    So, here goes:

    b = b XOR a

    a = b XOR a

    b = b XOR a

    1st line makes b a product of b XOR a

    2nd line makes a = a XOR (b XOR a) = b

    3rd line makes b = b XOR (b XOR a) = a

    Hope this helps (and is correct, feedback anyone?)
  • 1
    Uhm, this was weekly rant, not question 😅
  • 3
    @aviophile Ooops....

    I got ahead of myself.
  • 4
    @bladedemon btw simpler answer is;
    a = a + b
    b = a - b
    a = a - b
  • 1
    @bladedemon @aviophile

    Assuming OP's talking about numerical variable values, your solutions assume that inputs are within the domain of whole numbers. They'll fail if one of the two numbers is negative.

    I'd suggest clarifying the problem to whoever posed the challenge.
  • 2
    Easy. Use a permanent static/global variable

    this introduces a critical section, so handle it accordingly with locks
  • 0
    @netikras Or just don't use variables at all
  • 0
    @Ranchu meeh.. It's exactly what they expect you to do. Be creative!
  • 5
    Explain to me why this would be a "good" thing to know how to program? Most of the "tricks" I have seen are some math trick to store the data of both vars in one temporarily. This is not good code. It is absolutely shit code. So why would this ever be in interview question?
  • 0
    @Demolishun Optimization problem. Knowing how to improve efficiency at the cost of maintainability doesn't hurt, as long as we know when to.

    Although on certain compilers, they're smart enough to handle this exact optimization for us, so we can get away with declaring a temporary variable without worrying about memory overheads, just with the right compilation flags.
  • 0
    So, I have to ask, from an industry standpoint... Why would you need to do this?
  • 3
    @stonestorm You wouldn't. At best its second guessing the compiler/interpreter. At worst its creating obfuscated code.

    Maybe, just maybe, it might have a place in extremely critical code. Perhaps GPU code, but I have never had a reason to use this in GPU code. Nor in embedded code. Even in assembly language I wouldn't have done this. It makes huge assumptions about the data stored in the variable and would be vulnerable to overflow/underflow. It is stupid code.
  • 1
    @specialCardinal Nope, XOR works for any memory region, even floats or arrays/structs. You just have to pad them to equal length.
  • 1
    @Demolishun With the xor method you can swap gigabytes of data without allocating a third one.
  • 1
    @Lor-inc Then swap the pointers/references.
  • 0
    @Demolishun Obviously you won't swap gigabytes when there's complete data locality. This works for swapping out harddisks for example.
  • 7
    That's easy.

    Instead of:

    a = 1
    b = 2

    do

    a = 2
    b = 1
  • 2
    *obligatory Python solution*
  • 0
    @Lor-inc Yes, I know. I just like to bring up a point that it won't work on certain numerical inputs which wouldn't be the case for, say, objects.
  • 2
    @RANTSMCPANTS the only sane comment under this post.
  • 1
    @specialCardinal What numeric inputs would break the xor method?
  • 0
    @Lor-inc Now that I think of it, XOR indeed does the job lol. Don't know how it went over my head. Even I'm surprised with how dumb I can get sometimes 🤣. Thanks for the correction!
  • 1
    @specialCardinal @Lor-inc You are welcome :)

    Ahh, this is why I LOVE devrant... someone posts a joke, we take it seriously (because it is programming-related) and a great debate follows, resulting in full-blown war.

    Gotta love this place :P
  • 0
    I did a similar thing for my snake game on tm4c platform. Wanted to randomly generate food positions on map. After powerup, I made the game start when user pressed both buttons and used time difference between powerup and button push as random’ s seed.
Add Comment