Sunday, March 6, 2016

Swap two numbers using XOR operator(Bitwise operator in C)


#include <stdio.h> void main() { int i = 65; int k = 120; printf(" value of i=%d k=%d before swapping", i, k); i = i ^ k; k = i ^ k; i = i ^ k; printf("value of i=%d k=%d after swapping", i, k); }

Explanation:

 find binary equivalent of i=65
find binary equivalent of k=120

Then perform bitwise xor operation on each bit of i & k
i,e, i XOR k
execute all the statements and check the answer..

No comments:

Post a Comment