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 9260771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:58:43+00:00 2026-06-18T12:58:43+00:00

In python, I one had to swap values of 2 variables, all you need

  • 0

In python, I one had to swap values of 2 variables, all you need to do was

x,y=y,x

One can look at it as if the two statements-(x=y) and (y=x) are executed in parallel and not one after another.

Is there any way to achieve the same effect in c++?

NOTE/EDIT:

I am looking to extend this ‘parallel effect’ (if it exists) to more complicated expressions like
ones,twos= (ones ^ n) ^ ~twos, (ones & n) | (twos & ~n);

This is possible in python, is it possible in c++?

CONCLUSION:

So according to the answer given by leemes and the comments on his answer:

1.You can use boost libraries in C++03 or

2.You can use C++11

to access the std::tie and std::tuple to achieve this ‘parallel’ effect.
As for the present, I mark leemes answer as accepted but I’ll still be looking for methods to implement this cool functionality in C++03.

  • 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-18T12:58:44+00:00Added an answer on June 18, 2026 at 12:58 pm

    Special case: Swapping the value of two variables

    (For the general solution, see below.)

    To swap two variable’s values in C++, you should always use swap:

    using std::swap;
    swap(x, y);      // Do NOT say:  std::swap(x, y)    -- Read about Koenig lookup!
    

    Don’t bother how it will do it; it will do it very fast. The implementation of the C++ standard library will do its best to optimize this to a single instruction if the processor supports it (but the standard doesn’t tell the implementation to do so). For register-only variables, there is for example the x86 instruction xchg which will do it as fast as possible. Don’t try to tweak it with some “three xor operations”, it won’t be any faster. If you’re unlucky, it will not be optimized to something like xchg.

    The generic swap operation in C++03 introduces a temporary variable and performs three copy constructions. In C++11 there are move-semantics and the objects are rather moved than copied. For your own types, let’s say some data structure which holds only a pointer to the actual data, you should optimize this procedure to make it perform in constant time:

    • In C++03, you can either specialize std::swap or implement your own swap function in your namespace (see the two top answers on this question) to optimize swapping: Just swap each member in your class to swap their data. For the data structure example holding just a pointer, just swap the pointers.

    • In C++11, there are the new move semantics, which allow you to implement movement of the data from one to another object, which will result in a very similar behavior. (Move semantics have been introduced for more general problems like swapping two objects: If one object isn’t needed anymore, but another one has to be a “copy” of the first, it can simply be moved.) Read about move semantics and move constructor for details.

    • For both C++03 and C++11 there is an alternative way: You can implement implicitly shared data and copy-on-write for heavy classes like data structures. In the example above, where your data structure holds a pointer to the actual data, implement reference counting. When copying the data structure, just increase the reference counter by one. When modifying the data, make sure that it isn’t shared (ref count = 1), otherwise “detach” it by copying it only then. This results in a constant-time copy and swap operation.


    General case: Multiple arbitrary expressions

    For other statements which are not input/output dependent, like (a, b) = (x, y), just write them “as is”, and it will run at least perfectly pipelined since they don’t have any dependency:

    a = x;
    b = y;
    

    If they are input/output dependent, like your example in the edit, you can split it up and introduce temporaries. You won’t do yourself a favor by trying to solve such with some fancy expression tricks like xor-ing. The compiler knows a lot of tricks for assembler (like xchg), you only know tricks to express such in plain C++ (like xor).

    In C++11, there is std::tuple and std::tie allowing you to assign multiple expressions without introducing temporaries (they will be introduced behind the scenes to hold the values stored in the tuple and tries to optimize them either fully away or at least only using registers to hold them if possible):

    using std::tie;
    using std::make_tuple;
    tie(ones, twos) = make_tuple((ones ^ n) ^ ~twos, (ones & n) | (twos & ~n));
    

    Note that the types of the right hand side pair / tuple has to match the target values on the left hand side as conversion isn’t implicit here. If you encounter problems, perform a static_cast on the right hand side, tell std::make_tuple the explicit types or just use the constructor for std::tuple requiring explicit types, like:

    using std::tie;
    using std::tuple;
    tie(ones, twos) = tuple<int,int>((ones ^ n) ^ ~twos, (ones & n) | (twos & ~n));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone recommend a Socket.IO client library for Python? I've had a look around,
I had 2 Python similar scripts, that I've since merged into one (and now
I had recently started learning Python, and with all the research I decided it
I had two versions of Python installed on my machine (versions 2.6 and 2.5).
One of the great features of Django is that you can open a python
Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return
To date I had been developing only small Python scripts. They were not longer
Obs: I know lists in python are not order-fixed, but think that this one
so i've begun google's python class, and have not had that much difficulty with
I use Python in one of my products. I compiled the source code using:

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.