Hey guys,
I thought some of you might find this trick useful.
say you have 2 integers
int a = 5;
int b = 3;
Say you need to switch these variables so that a = b and b=a.
Most people would od it such that
However, there is a way to do this in which a third variable is not needed.Code:int a=5; int b=3; int c; c=a; a=b; b=c;
That's it.Code:int a=5; int b=3; a ^= b; b ^= a; a ^= b;
You just successfully switched the values of a and b without a third variable. Thank God for Boolean Logic :-D
Note: This was done using integers but it could also be done using any data type including characters and structs because this command does not really care. All it cares about is the binary data.