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
-
'90's C linux developers?
Lol, regex is nice in many ways and bitwise saves code space when it comes to get rid of decimals 😋 -
@Root
Something like the following has way better readability and can be done in all somewhat modern languages:
a, b = b, a -
Root797634y@Oktokolo Something like that requires an allocation and therefore is incredibly slow by comparison.
-
@Root
Most of the time, clean semantics are more important. Let the compiler do femto optimizations like that. -
@Oktokolo The semantics _are_ clean. If you’re worried about readability, I would argue that the a, b = b, a convention is just as confusing if you haven’t been previously exposed to it. And which approach is appropriate to use depends on what constraints you’re working under.
-
Root797634y@Oktokolo Readability is definitely important. However, many languages that allow similar syntax to that are interpreted, not compiled, meaning there are very few optimizations applied to the runtime 🙂
Regardless, though, switching the contents of variables is just bizarre in practice anyway. I’ve needed to in only a handful of instances, and looking back I’m sure there was a better approach I wasn’t aware of. -
@Root It's pretty common in sorting algos.
I've never used xor for swapping in reality, only the triangular method. However, xor may make sense for native word size swaps on register starved architectures like x86-32. -
Root797634y@Fast-Nop I haven’t either. I don’t remember swapping vars outside of school, honestly. And that was mostly C/C++/Java where it didn’t matter. 🤷🏻♀️
-
hjk10156964y@Oktokolo I never understood why the fuck there is so much effort in swapping vars. I have never had to use it in all of my career. Including in stats and pivoted graphs etc. You just have another routine that takes X and Y that are just that inside the routine. Inside the same routine just declare 4 variables that actually say what the fuck they are for.
This is why I love go by the way. A modern language but without all the BS features noone really needs. -
@hjk101
I just almost used variable swapping in an iterative approximation algorithm.
But i then ended up adding the previous value to the tuple containing other state data.
Swaps are indeed almost never needed in high-level code. But i remember using a few in C64 and x86 assembly back then when registers were sparse and the accumulator was still special...
That said: I am certain, that swaps are only really used in first naive approaches on algorithms outside the hand-optimized assembly world.
Both uses are fine and going for maximum performance for the latter use case, is actually fine too.
I doubt, that anyone really casually uses an xor swap in Python or Java code. And the few that use it in C++ are probably doing so more for showing off their micro-optimization skills or satisfying their inner child than anything else.
So no, there isn't that much effort in swapping vars.
And tuple assignments have far better uses than swapping vars. They just can do that too. -
@Oktokolo wow, nice idea using tuples. In some languages tuples are extremely light weight, so this might also be almost as efficient as xor.
I like xor but tuples in this case is much more readable. You can literally see how the values are swapped. You can not be any more clear without using a function or macro.
Related Rants
-
electronix9It happened! It finally happened. I got to use the XOR operator today, for the first time in my 10 years of d...
-
Aiei16Manager: Hey, this is Junior. he will work with you from now on. Me: Oh cool, we could use some help. (moments...
-
Bramskyyy5For my fellow javascript devs: var floored = 12.68 | 0; Is much faster than: var floored = Math.floor(12.68...
I learned how to solve some problems using XOR.
This might be like when I learned to love regex.
*loud laugh reminiscent of Vincent Price in the Thriller music video*
What am I becoming?
rant
xor
bitwise
madness