LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#51 June 19 2010

nuclearcat
Member

Re: C Programming Final Exams

rolf wrote:

@nuclearcat, you have a problem in your code,
I think the 3rd line:

       array[i] ^= array[length-1-i];

Is a mistake, and it's the same then the 1st one.

It's f*** crazy though, bitwise XOR can actually reverse the array :)
I did it with a paper and a pen, and it works! Took me 1/2 hour to understand.

From wiki

Conventional swapping requires the use of a temporary storage variable. Using the XOR swap algorithm, however, no temporary storage is needed. The algorithm is as follows:

X := X XOR Y
Y := X XOR Y
X := X XOR Y

length-1-i is also correct, so when i have index value 0, i will use to swap it with last element (since count is from 0, then it is length-i-1

Offline

Board footer