Thread: Swap two variables with out third variable

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Do a web search for xor swap.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    Hi Kanthi,

    Swapping 2 variables is possible as follows:

    x = x + y
    y = x - y
    x = x - y

    Although this way doesn't require a third variable, but it is not simple to understand. So I think it is not good to use this way.


    K.n.i.g.h.t

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swap of variables without tmp
    By ZaC in forum C Programming
    Replies: 16
    Last Post: 06-13-2009, 02:28 PM
  2. changing the address of a pointer question..
    By transgalactic2 in forum C Programming
    Replies: 42
    Last Post: 10-16-2008, 09:20 AM
  3. Variable Call via Another Variable's Content
    By E-Rac in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2005, 01:51 PM
  4. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM