Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

swapping program does not use third variable

Answer Posted / rajeshwar raja

Swap(int* a, int* b)
{
(*a) ^= (*b) ^= (*a) ^= (*b);
}

Explanation:
Expressions are evaluated from right to left.
Take the right most operation (*a) ^= (*b), its is short
hand XOR operation between 'a' and 'b'.
Assume a = 2 and b = 3.
a = 2 ^ 3 = 1
Next operation (*b) ^= (*a),
b = 3 ^ 1 = 2 (Note a is 1 now) ***(b is 2)***
Next operation (*a) ^= (*b)
a = 1 ^ 2 = 3 (Note b is 2 now) ***(a is 3)***

SWAPPED!!!
It can swap complex data structures also.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is stream in oop?

1297


What is difference between abstraction and encapsulation?

1070


What are the components of marker interface?

1049


i am getting an of the type can not convert int to int *. to overcome this problem what we should do?

2313


What is the types of inheritance?

1091


Why is object oriented programming so hard?

1075


What is ambiguity in inheritance?

1108


Why is encapsulation used?

988


What is encapsulation in oop?

1042


What is oops concept with example?

1011


What is static modifier?

1211


What is polymorphism what is it for and how is it used?

1012


What is byval and byref? What are differences between them?

2210


What are main features of oop?

1119


What is encapsulation with real life example?

1032