Swap value except third variable.

27 Jun

Idly I was passing my time. But it was interesting.

It is a simple problem but all try was failing each time. Finally I thought it is impossible and this universal.

Finally I could solve it partially…

Look. I have tow value in two variables and I want swap it except third variable…!

Oh. I am no crazy. But is it possible.

Yap, this is. A simple process if I apply a mathematic rule.

A = 5
B = 7
A = A+B => 5 + 7 = 12
B = A – B => 12 – 7 = 5
A = A –B => 17 – 5 = 7

Wow… Really! Very easy.

But this is not 100% solution. Because it fail if it is string type value
But I am thinking if we can access the ASCII code. Because all ASCII are integer value.


2 Responses to “Swap value except third variable.”

  1. Fuad Ahasan Chowdhury June 30, 2007 at 11:04 am #

    hehe.. interesting mama.. 😉


  2. Joao Ferreira July 11, 2007 at 3:13 pm #

    Hi. To switch any two variables, you can use the bitwise exclusive-or.
    I wrote a short note about this. You can find it at:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: