Hi!
How dose this code work?
what dose it mean?
/* C trick to swap n1 and n2 */
n1^=n2^=n1^=n2;
Hi!
How dose this code work?
what dose it mean?
/* C trick to swap n1 and n2 */
n1^=n2^=n1^=n2;
Is this a homework assignment?
I'll give you a hint. Assuming n1 and n2 to be integral types (char, int, long, unsigned), you're doing exclusive OR operations. Work it out on paper with ones and zeros. Remember that evaluation is grouped from right to left.
Rewriting this into separate lines, we get:
n1 ^= n2;
n2 ^= n1;
n1 ^= n2;
Rewriting again,
n1 = n1 ^ n2;
n2 = n2 ^ n1;
n1 = n1 ^ n2;
One property of the bitwise xor operator is that (A ^ B) ^ A = B. And remember, it is commutative, i.e. A ^ B = B ^ A.
Let n1 = A and let n2 = B. Then we get
n1 = n1 ^ n2; /* Assigns A ^ B to n1. n2 still equals B. */
n2 = n2 ^ n1; /* Assigns B ^ (A ^ B) to n2. So n2 equals A, n1 equals (A ^ B) */
n1 = n1 ^ n2; /* Assigns (A ^ B) ^ A to n1. So n1 equals B. */
The end result is that n1 and n2 have been xor'ed through each other.
Note that this only works if n1 and n2 are distinct numbers. I.e. *p ^= *q ^= *p ^= *q fails miserably if p and q are pointing at the same number. It's generally better to use a third-party, temporary variable. Using a third-party variable works for 'all' datatypes (I'm talking about C++ here), it makes your code more readable, and it makes your code more generalized, instead of relying on the particular behaviors of integers.
Compilers with optimisations turned on can recognize both forms of swapping, so one is not necessarily faster than the other.
the ^ operator xors the bits:
1^0 = 1
0^1 = 1
0 otherwise
This statment can be exapnded as follows (from right to left) :
n1 = n1 ^ n2;
n2 = n2 ^ n1;
n1 = n1 ^ n2;
For Example if n1 = 30 = 0001 1110 and n2 = 21 = 0001 0101
then:
n1 = n1 ^ n2 = 0000 1011
n2 = n2 ^ n1 = 0001 1110
n1 = n1 ^ n2 = 0001 0101
n1 = 21; n2 = 30
No, is not a homework assignment.
I try to understand this program.
Code:#include <stdio.h> /* Function f(n) returns the n'th Fibonacci number * It uses ALGORITHM 2C: NON-RECURSIVE LOOP */ unsigned int f(int n) { int i; unsigned int n1=1,n2=1; for(i=1; i<=n; i++) { n1+=n2; n1^=n2^=n1^=n2; /* C trick to swap n1 and n2 */ printf("%d",n1); } return n1; } /* Function f_print(n) prints the n'th Fibonacci number */ void f_print(int n) { printf("%dth Fibonacci number is %lu\n",n,f(n)); } /* Function main() is the program entry point in C */ int main(void) { f_print(20); return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
That thread doesn't show the example where the xor's are chained together in a single expression. I should also point out that a ^= b ^= a ^=b is undefined due to multiple side effects:Originally Posted by Dave_Sinkula
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."
Dang. I was really hoping for folks to hit on this:
For those who missed it, repeat after me: "we're not in the 70's, we're not in the 70's, ..."Originally Posted by Salem
'Cuz even if you "correct" it, it still sucks.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
*puts down flame thrower for another thread*
Not to mention http://www.eskimo.com/~scs/C-faq/q10.3.html
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.