Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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?) -
@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. -
Easy. Use a permanent static/global variable
this introduces a critical section, so handle it accordingly with locks -
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?
-
@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. -
So, I have to ask, from an industry standpoint... Why would you need to do this?
-
@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. -
@specialCardinal Nope, XOR works for any memory region, even floats or arrays/structs. You just have to pad them to equal length.
-
@Demolishun With the xor method you can swap gigabytes of data without allocating a third one.
-
@Demolishun Obviously you won't swap gigabytes when there's complete data locality. This works for swapping out harddisks for example.
-
@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.
-
@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!
-
@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 -
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.
Related Rants
-
dfox3My favorite kind of interview question/challenge is anything that is highly practical for the job. At the curr...
-
opengenus7Interviewer: How will you solve the travelling salesman problem? Me: *explains the solution on whiteboard* I...
-
karma11Please make an entire webshop with animated shopping cart in react + redux within a week 👍 We will then re...
How to swap two variables’ values without a temporary variable. I failed :/.
rant
wk192