Xor c - need an explanation

can someone explain to me that XOR, (^)making in summary exaclly code and how is it that the function is a pointer?

char *strReverse(char *str)
{
      char *pb, *pe;

      for (pb = str, pe = str + strlen(str) - 1; pe > pb; ++pb, --pe)
      {
            *pb ^= *pe;
            *pe ^= *pb;
            *pb ^= *pe;
      }
      return str;
}
+3
source share
2 answers

The function is not a pointer, but returns char*.

The function changes the line.

This method is XORused to replace two elements without additional memory. As you can see, the loop repeats through the beginning and end of the line and swaps the two chars.

+3
source

using xor like this is another way to exchange two values ​​in memory without using a temporary variable. I recommend bit hacks for other bit hacks

+1
source

All Articles