You're viewing a comment by Sherif and its responses.

Sherif Permalink
May 03, 2010, 15:15

And also here how to swap two vars without a temp var using bit wise operations:

i will write it in C
void Swap_Integers(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}

that is it and yeah it is obvious a little but here how it goes it depend on the concept that two XOR operations on the same var can be cancelled f.e. a ^ b ^ b = a
so first i put the XOR of both in x (x = x^y) now then to put x in y you just need another XOR with x (y = x ^ y ^y) and after that x will equal ( x ^ the new y (holding x inside) to be y ^ x ^x ) have fun and you can use this concept to make encryption also and i will leave that as an exercise to you ;) by the way nice work my friend peter

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "server_143": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.