Use the Exclusive OR operator ^ for example like this:Quote:
Originally Posted by drdepoy
Note: Study this old classic trick to let two numbers exchange places!Code:int a, b, x;
a = 111;
b = 0xF6E; /* Hexadecimal literals can be stored by using 0x */
x = a ^ b; /* x now contains the value of a XOR b */
Mod Edit: this topic split from http://cboard.cprogramming.com/showthread.php?t=72638Code:int main( ) {
int a, b;
a = 1234;
b = 5678;
printf( "a = %i, b = %i\n", a, b );
a ^= b ^= a ^= b; /* Study how this line works! */
printf( "a = %i, b = %i\n", a, b );
return 0;
}