Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:19:22+00:00 2026-06-05T09:19:22+00:00

I have two integer variables i and j and I want to make a

  • 0

I have two integer variables i and j and I want to make a function which takes these two variables as its argument and interchanges their contents using xor operator. Now if I make the function to take arguments by value i.e void swap (int x , int y);(with function body same as for the function swap below) then the values are being swapped nicely within the function. But as what I want is the swapping of the values of the variables in the calling function I used passing arguments by reference (and by pointers as well) :

void swap ( int& x , int& y )
{
    x^=y^=x^=y;
    cout << x<< " " << y << endl ;
}

int main ()
{
    int i (1), j (2) ;
    swap ( i, j ) ;
    cout << i << " " << j << endl ;
}

but neither case showed the correct result !

Why is the swap function not swapping the values on passing arguments by reference or pointers ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:19:23+00:00Added an answer on June 5, 2026 at 9:19 am

    I have two integer variables i and j and I want to make a function
    which takes these two variables as its argument and interchanges their
    contents using xor operator.

    Why?

    As you’ve figured out, you need either to pass pointers to the objects, or to use references.

    This:

    x^=y^=x^=y;
    

    has undefined behavior, since it modifies the same object twice between sequence points (it does this for both x and y).

    The xor trick fails if both objects are the same object; the first xor zeros the object.

    If you want to swap two objects, just use a temporary:

    int temp = x;
    x = y;
    y = temp;
    

    It’s simple, and it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two integer variables which I need to divide in order to work
I have two or more variables of class object in c# which has integer
I have two variables MAX_TABLE_ID INTEGER; NEXT_TABLE_ID INTEGER; I'm reading values into these and
In java i have two variables String string; int integerValue; I want to convert
Let's say I have any array Trial() As Integer I have two following variables
I have two variables : count, which is a number of my filtered objects,
In JavaScript, I want to write a function that determines if the two variables
I want to swap two integers, and I want to know which of these
I have two Integer variables like int Max = 10; int limit = 5;
I have two integer values, x and total. I am trying to find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.