Blog about stuff and things and stuff. Mostly about SQL server and .Net

## Reverse string in C# and bitwise XOR

why is there no reverse string function in c#?? Is there realy no need for that function??
there are of course a few ways to make your own reverse function. most favorite is definitly the one using recursion with substring. that's preety cool i thought why not… and then i remembered something that i learned in my first year of college… bitwise operations. i knew that there was a handy little way of switching 2 values using something with XOR without using a 3rd variable. i just couldn't remember it… so i went googling… it sure was faster than going through my books :)) and voila… XOR 2 values bitwise 3 times and they're switched.
So i metion this to a friend of mine… a soon to be a computer science graduate… and he looks at me with a blank stare… so i go about explaining it and he goes: huh??
so why are bitwise operation so hard to understand?? are they too hard to grasp???
well for anyone who wants it :) here's the reverse function:

```public string Reverse(string str)
{
// convert the string to char array
char[] charArray = str.ToCharArray();
int len = str.Length - 1;
/*
now this for is a bit unconventional at first glance because there
are 2 variables that we're changing values of: i++ and len–.
the order of them is irrelevant. so basicaly we're going with i from
start to end of the array. with len we're shortening the array by one
each time. this is probably understandable.
/
for (int i = 0; i < len; i++, len–)
{
/
now this is the tricky part people that should know about it don't.
look at the table below to see what's going on exactly here.
*/
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}
return new string(charArray);
}
```

OK, so to clear up for those who are stumped at how this works:
let's say we want to switch 2 binary values x1 = 100 and x2 = 111 so that x1 = 111 and x2 = 100.

in c# x1 ^= x2 equals to x1 = x1 ^ x2 where ^ is the bitwise XOR.

 XOR (Exclusive OR) table 0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0
 First operation: x1 = x1 XOR x2 x1: 1 0 0 x2: 1 1 1 New x1: 0 1 1 Second operation x2 = x2 XOR x1 x1: 0 1 1 x2: 1 1 1 New x2: 1 0 0 Third operation: x1 = x1 XOR x2 x1: 0 1 1 x2: 1 0 0 New x1: 1 1 1

And voila, we now have x1 = 111 and x2 = 100.
and for each pair in array we do this because string chars are in essence binary values :)))
This method is also the quickest way to reverse a string.
Recursive way is almost 4-5 times slower.

EDIT:

Well after Arnold pointed me to this site i came up with this:

```public string Reverse2(string x)
{
char[] charArray = new char[x.Length];
int len = x.Length - 1;
for (int i = 0; i <= len; i++)
charArray[i] = x[len-i];
return new string(charArray);
}
```

This one is even faster than XOR on small as well as large strings by 50%. Looking at the algorithm is preety evident where the speed is gained:

here there's no copy of the string it's just a new empty array of fixed length
```char[] charArray = new char[x.Length];
```

there are no 3 operations just one. i++ and len– in XOR function is equalized by i++ and len-i here so the speed is just from 1 operation vs. 3 operations.
```for (int i = 0; i <= len; i++)
charArray[i] = x[len-i];
```

Arnold if you have any check for surrogate pairs you can share i'm all ears (eyes). :))  