GeeksforGeeks » Interview Questions

Swapping two nos without arithmetic and logical operators

(4 posts)
  • Started 10 months ago by priyankaiyer
  • Latest reply from ayyappa
  1. priyankaiyer
    Member
    Posted 10 months ago #

    Tis was asked for my frend in thought works interview..... How to swap two numbers without using a temporary variable, logical and arithmetic operators?

  2. rajcools
    Member
    Posted 10 months ago #

    bitwise xor

  3. Agniswar
    Member
    Posted 10 months ago #

    yeah bitwise xor would do..this is the algorithm for your help..

    swap(int x , int y)
    {
    x=x xor y;
    y=x xor y;
    x=x xor y;
    }

  4. ayyappa
    guest
    Posted 4 months ago #

    a=(a+b)-(b=a)


Reply

You must log in to post.

RSS feed for this topic