How to Swap Two Variables Using XOR

Поделиться
HTML-код
  • Опубликовано: 23 янв 2025

Комментарии • 12

  • @mattias3668
    @mattias3668 4 года назад

    Commutativity only matters depending on how you write the statements.
    What really matters is associativity: that you can rewrite (A ^ B) ^ B as A ^ B ^ B and then A ^ (B ^ B) which gets you A ^ 0, which is A.
    XOR swap is nice because you can write it as 'A ^= B ^= A ^= B', which is one printable character and two spaces shorter than 'T = A, A = B, B = T'.

  • @oliverhenning8758
    @oliverhenning8758 3 года назад +1

    There is an even better version of this that works with all numerical types:
    A = A + B
    B = A - B
    A = A - B

  • @oneplusone5763
    @oneplusone5763 8 лет назад +1

    Hey,
    how did you show line by line execution of program?
    what kind of IDE are you using?
    I want to see line by line execution of large python scripts.

    • @WhatsACreel
      @WhatsACreel  8 лет назад

      It's visual studio. Free IDE from MS, search for "Visual Studio 2015 Community edition" and you should find it. They've recently added python to it, but you might have to install it as an extension. Good luck and thanks for watching!

  • @moezart94
    @moezart94 7 лет назад

    Thank you for the video! very informative. But I have one question, is there a reason why using the same value did not result in zero when using non-pointer values? I tried using a = b, but the result was just fine.

    • @WhatsACreel
      @WhatsACreel  7 лет назад

      In that case, A^B would be zero, but since they're both the same value, you still have the value saved. When you XOR again, you get it back. If you're using pointers, you overwrite the only value you have, then when you XOR again, you're just XORing two 0's. Hope that makes sense, have a good one and happy new year!

    • @moezart94
      @moezart94 7 лет назад

      Thank you so much for the reply! definitely makes sense.
      Hope for you a happy new year too!

  • @ryanjackson0x
    @ryanjackson0x 10 лет назад

    Does this work without integers?

    • @WhatsACreel
      @WhatsACreel  10 лет назад +3

      It works with floats and any other data. But C++ is typesafe and it's not good at using the bits of a float as an int. If you're using Assembly, it's easy since Assembly doesn't care about data types. But in C++ there's too much wrangling involved with treating the bits as an int without casting them.
      Have a look a the code below, it performs the XOR swap between two floats:
      float a = -12.8f, b = 4.3f;
      int* A, *B; // tmp vars for int versions
      // Point to bits of a and b without casting
      A = (int*)&a;
      B = (int*)&b;
      // XOR swap
      *A = *A ^ *B;
      *B = *A ^ *B;
      *A = *A ^ *B;
      Cheers, have a good one!

    • @ryanjackson0x
      @ryanjackson0x 10 лет назад

      Thanks!! Great response.

  • @brisketbaron
    @brisketbaron 7 лет назад +2

    7:47

    • @troglodyto
      @troglodyto 4 года назад

      not all heroes wear capes!