23

Software development lessons are so boring and the teacher is so stupid. He can't swap two variables without a temporary var. He said that he never saw this kind of swaping before. I pay attention sometimes, but I'm just drawing in my exercise book.

Comments
  • 3
    Looking good looking good
  • 8
    How do you swap 2 variables without a temporary var?
  • 2
    $a = 5;
    $b = 6;
    list($a, $b) = array($b, $a);
    print $a . ',' . $b;
  • 10
    @AndSoWeCode depends on the exact language, but I've seen implementations that use a bitwise XOR to do it:

    x = x XOR y
    y = y XOR x
    x = x XOR y

    That said... Unless you work in systems programming, I feel like this kind of trivia is more or less useless. I've been a professional software developer for about 3 years now and have never seen a situation where swapping things was even needed, let alone swapping things within such tight constraints.
  • 5
    @localjoost that's a load of temporary variables.
  • 3
    @localjoost and it's very language-gnostic
  • 0
    @Zaphod65 that's not for variables, but for integers specifically.
  • 4
    Windows 🤢
  • 2
    Less lines of code don't mean less processing. Actually list($a, $b) = array($b, $a) implies an array creation and possibly iteration, which is even less efficient in terms of cpu and memory than an extra variable...
  • 0
    @Zaphod65 sorting algorithms do. Anyways, brilliant solution for swapping, might try some day.
  • 2
    In Python(and I think Rust) you can just write

    (a, b) = (b, a)

    I'm not sure if it's really more efficient than a temp variable, but it sure as hell looks clearer.
  • 1
    @Zaphod65 FFS I TRIED AND IT WORKS!!

    @Gradle now you can tell you teacher how to do it in any language. Using XOR bitwise operators.

    @localjoost this is the code in PHP by the way:
    $x = $x ^ $y;
    $y = $y ^ $x;
    $x = $x ^ $y;
  • 10
    a = a + b
    b = a - b
    a = a - b
  • 0
  • 3
    @ithinkihaversi has the answer. And thank you everyone! You surprised me with these 29 notifications. 😅😁
  • 2
    @ithinkihaversi MAN... EPIC RANT THIS ONE !!!!!
  • 2
    @ithinkihaversi that can very easily cause an overflow
  • 2
    The point is:

    THE way to interchange 2 values is by using a third. Everything else is worse:

    It either creates a lot more variables in the background
    OR
    It only works for binary data with the same format that support the XOR operation
    OR
    is even worse than all of these.

    Just because it's stupidly simple doesn't mean that it has to be changed.
  • 1
    I had this moment of "Are you even a good programmer?" to most other programmers when I first figured out how to swap two variables without using a temporary variable on my own (as a part of a challenge I read somewhere). The truth is, most great programers have never really given it a thought coz it's a useless thing to know really. @gradle, I hope you're not in this moment yourself. xD
  • 1
    @AndSoWeCode I'm not saying that this is the proper way to do it, some things are just brain teasers like this one!
  • 1
    If one likes brain teasers, one shouldn't waste time with such trivial teases, and instead learn Hodor
    http://www.hodor-lang.org/
  • 0
    @AndSoWeCode what isn't binary? Everything can be swapped with the xor method
    Unless you don't have access to pointers, in which case efficiency is probably not a concern for you
  • 0
    @AndSoWeCode That looks more like a joke than a brain teaser to me.
  • 0
    @AndSoWeCode brainfuck feels more like a brainteaser, more puzzle-ish
  • 0
    @rangler the vast majority of languages only support xor for integers. The reason is because it doesn't make sense to xor floating points since every bit means something else depending on another bit, and xor-ing strings is useless since they're just pointers and are of variable lengths. XOR on more complex data structures is even more ridiculous.

    That's why performing binary arithmetic or general arighmetic for swapping variables, in general, is simply wrong.
  • 0
    @AndSoWeCode well, thats kind of what i said you could do with an xor, it doesn't matter if the bits "make sense" like with integers. I do admit that it only applies to langs that have low level access, but if you need to save the 8bytes an auxiliary variable takes you are probably working low level.
    Strings aren't xorable nor assignable to a temp variable because they're pointers, just like more complex structures. You can store said pointer in a temp var but this could also be achieved with an xor.
    Cloning is another story.
    Anyway, fun discussion 😁
  • 0
    @rangler in modern languages strings are treated like immutable scalars, rather than pointers to arrays in memory. An assignment is always a copy.
  • 0
    @AndSoWeCode a string assignment is a copy of the pointer, a string pool is maintained to save memory
  • 0
    In python we just do:
    b, a = a, b
Add Comment