.NET and node.js programming: languages, patterns, technologies, frameworks.
Following code snippet is commonly used in C/C++ to exchange values of two integer values with no help of any additional variable.
int x, y;
x ^= y ^= x ^= y;
Before the expression is evaluated, argument values are pushed onto the stack, therefore assignments are only updating the variables, but not the expression arguments. The expression is carried out as follows:1. Push x, y, x and y onto the stack. The stack is:y0x0y0x0x is x0.y is y0.2. XOR the top two values and store them in x. The stack is:x0 ^ y0y0x0x is x0 ^ y0.y is y0.3. XOR the top two values and store them in y. The stack is:x0 ^ y0 ^ y0 = x0x0x is x0 ^ y0y is x04. XOR the top two values and store them in x. The stack is:x0 ^ x0 = 0x is 0y is x0
Post a Comment