[c][explained] The XOR Swap

Поділитися
Вставка
  • Опубліковано 5 вер 2024

КОМЕНТАРІ • 34

  • @MoMadNU
    @MoMadNU 2 роки тому +17

    This swapping problem was assigned to my son over spring break at his university. I have been in the industry 40+ years and was not aware of this method. Even in assembly programming we used a third register as a temp which is more efficient than XOR, and there is no edge case. When my son presented the XOR solution, the professor did not even know that a circumflex is the XOR operator let alone how to use it. I can't wait to see the professor's solution when the assignment explicitly said "without allocating any extra storage" 🤣

    • @theteachr
      @theteachr  2 роки тому +6

      Post the prof’s solution once they do show it. Although the one below is what I’ve most commonly seen:
      a = a + b;
      b = a - b;
      a = a - b;

    • @MoMadNU
      @MoMadNU 2 роки тому +6

      @@theteachr That's exactly what she provided. She failed to mention however that it can cause an overflow or underflow condition

  • @artasheschalabyan8490
    @artasheschalabyan8490 2 роки тому +2

    Very nice video, it explained things quite clearly in a way that other videos did not

  • @EzelBayraktar964
    @EzelBayraktar964 11 місяців тому

    omg cs 2nd grade student and this... this amazed me. extremely sophisticated work thanks bro

  • @arthur.s
    @arthur.s Рік тому +2

    Gorgeous visuals, the part that helped me was realizing that properties apply to propositional operations. I assume distributive property could also work?

    • @theteachr
      @theteachr  Рік тому +1

      It doesn’t distribute over addition.
      1 ^ (1 + 2) ≠ 1 ^ 1 + 1 ^ 2

  • @astrolemonade349
    @astrolemonade349 4 роки тому +5

    Awesome video ! What is the name of the monospaced font you used ?

  • @ExplosiveLizard
    @ExplosiveLizard 3 роки тому +3

    Darn that edge case is really bugging me, I can't seem to figure it out... Love your vids btw

    • @theteachr
      @theteachr  3 роки тому +3

      Stop reading this comment if you are still trying to figure it out on your own.
      What is a ^ a?

    • @ExplosiveLizard
      @ExplosiveLizard 3 роки тому

      @@theteachr a ^ a = 0... but I'm a little confused how this would ruin the data, because a ^ 0 = a so you can still retrieve the number

    • @theteachr
      @theteachr  3 роки тому +3

      @@ExplosiveLizard What do you think would happen if you call the routine on two different variables that had the same value?

    • @ExplosiveLizard
      @ExplosiveLizard 3 роки тому +2

      @@theteachr the result would be 0. But I tried running the code in a variety of languages and the variables stay the same after the executing the expressions

    • @thatcrockpot1530
      @thatcrockpot1530 3 роки тому

      @@theteachr so in C:
      void xorswap(int a, int b) { ... }
      (a != b) ? xorswap(a, b) : NULL;
      Not ansi compliant btw

  • @TechByte8
    @TechByte8 Рік тому

    Very well explained.

  • @senzmaki
    @senzmaki 2 роки тому +3

    @theteachr I'm so confused like if a and b were 9 like wouldn't this happen
    a = 9^9 = 0
    b = 9^0 = 9
    a = 9^0 = 0
    so nothing would change??

    • @theteachr
      @theteachr  2 роки тому

      I need to &my_mistake. Your last swapping line should have been `a = 9 ^ 0 = 9`, by the way.

    • @senzmaki
      @senzmaki 2 роки тому

      @@theteachr thanks I don't know how I even made such a simple mistake

    • @theteachr
      @theteachr  2 роки тому +2

      The point you’re (I did too) missing is that a and b are pointers. When a and b are the same, it means that they’re pointing to the same variable. The first statement will make the variable 0. b in the next statement will also be 0 as it was pointing to the same location.

    • @senzmaki
      @senzmaki 2 роки тому +1

      @@theteachr Ok I see now, thanks

    • @IrfanAli-so5hh
      @IrfanAli-so5hh Рік тому

      You opened my eyes bro(*´︶`*)♡Thanks!

  • @gauravsharma3444
    @gauravsharma3444 2 роки тому

    Well explained 🔥🔥

  • @fadiashraf5181
    @fadiashraf5181 2 роки тому

    Thank you !!!
    ❤❤

  • @SANDVlCH
    @SANDVlCH 3 роки тому +2

    Isn’t the edge case when a and b are equal?

  • @vinyasns
    @vinyasns 4 роки тому

    2nd line should have been b = b ^ a?

    • @theteachr
      @theteachr  4 роки тому +6

      XOR is commutative. Works fine, unless I'm wrong.

    • @vinyasns
      @vinyasns 4 роки тому

      @@theteachr right, my bad