28

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?

Comments
  • 9
    '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 😋
  • 10
    a ^= b
    b ^= a
    a ^= b
  • 5
    @Root The old swap without using another variable trick! I love this one. :)
  • 4
    @Root
    Something like the following has way better readability and can be done in all somewhat modern languages:
    a, b = b, a
  • 3
    @Oktokolo Something like that requires an allocation and therefore is incredibly slow by comparison.
  • 4
    @Root
    Most of the time, clean semantics are more important. Let the compiler do femto optimizations like that.
  • 6
    @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.
  • 3
    @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.
  • 2
    @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.
  • 4
    @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. 🤷🏻‍♀️
  • 1
    @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.
  • 0
    @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.
  • 0
    @Root Python has a dedicated fast bytecode instruction to do just this without allocation. (I'm guessing you're hinting at Python by the way)
  • 0
    @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.
Add Comment