Day#78 'C' Coding Challenge

Day#78 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> void main() { int var1 = 1, var2 = 2; var1 ^= var2 ^= var1 ^= var2; printf("var1 = %d,var2 = %d",var1,var2); }

----------------------------------------------------------------

The above code is another way to swap two variables. The operation done is bitwise XOR.

The sequence of execution of this expression is as follows:

var1 = var1^var2   ---- var1 = 0001 XOR 0010 = 0011

var2 = var2^var1   ---- var2 = 0010 XOR 0011 = 0001

var1 = var1^var2   ---- var1 = 0011 XOR 0001 = 0010

The output is var1 = 2, var2 = 1

The other C challenge questions can be accessed from below link:

http://forum.talenteve.com/viewforum.php?f=46

Post a Comment

0 Comments