C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

Write a C program to swap two numbers using bitwise operators?

0 votes
33,441 views
asked Jan 8, 2014 by keem Expert (13,240 points)

1 Answer

0 votes
Program:

#include <stdio.h>

int main() {

int i = 65;

int k = 120;

printf("\n value of i=%d k=%d before swapping", i, k);

i = i ^ k;

k = i ^ k;

i = i ^ k;

printf("\n value of i=%d k=%d after swapping", i, k);

return 0;

}

Explanation:

i = 65; binary equivalent of 65 is 0100 0001

k = 120; binary equivalent of 120 is 0111 1000

i = i^k;

i...0100 0001

k...0111 1000

---------

val of i = 0011 1001

---------

k = i^k

i...0011 1001

k...0111 1000

---------

val of k = 0100 0001 binary equivalent of this is 65

---------(that is the initial value of i)

i = i^k

i...0011 1001

k...0100 0001

---------

val of i = 0111 1000 binary equivalent of this is 120

--------- (that is the initial value of k)
answered Jan 8, 2014 by rajesh Guru (39,140 points)

Related questions

0 votes
1 answer 1,536 views
+1 vote
3 answers 6,747 views
6,747 views asked Jul 13, 2011 by Smita Advisor (7,140 points)
0 votes
1 answer 114 views
0 votes
1 answer 69 views
0 votes
1 answer 776 views
...