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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:51:16+00:00 2026-06-05T00:51:16+00:00

Simply, X = Integer Y = Another Integer Z ( If used ,Integer Temp

  • 0

Simply,

X = Integer
Y = Another Integer

Z ( If used ,Integer Temp )

What’s the most efficient method ?

Method I :

Z = X
X = Y
Y = Z

Method II :

X ^= Y
Y ^= X
X ^= Y

Edit I [ Assembly View ]

Method I :

MOV  
MOV  
MOV

Method II :

TEST ( AND )  
JZ  
XOR  
XOR  
XOR

Notes :

  • MOV is slower then XOR
  • TEST , JZ is used for XOR Equality Safe
  • `Method I uses extra register
  • 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-05T00:51:17+00:00Added an answer on June 5, 2026 at 12:51 am

    In most cases, using a temporary variable (usually a register at assembly level) is the best choice, and the one that a compiler will tend to generate.

    In most practical scenarios, the trivial swap algorithm using a
    temporary register is more efficient. Limited situations in which XOR
    swapping may be practical include: On a processor where the
    instruction set encoding permits the XOR swap to be encoded in a
    smaller number of bytes; In a region with high register pressure, it
    may allow the register allocator to avoid spilling a register. In
    microcontrollers where available RAM is very limited. Because these
    situations are rare, most optimizing compilers do not generate XOR
    swap code.

    http://en.wikipedia.org/wiki/XOR_swap_algorithm

    Also, your XOR Swap implementation fails if the same variable is passed as both arguments. A correct implementation (from the same link) would be:

    void xorSwap (int *x, int *y) {
         if (x != y) {
             *x ^= *y;
             *y ^= *x;
             *x ^= *y;
         }
     }
    

    Note that the code does not swap the integers passed immediately, but
    first checks if their addresses are distinct. This is because, if the
    addresses are equal, the algorithm will fold to a triple *x ^= *x
    resulting in zero.

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

Sidebar

Related Questions

I'm simply trying to convert a CGEventTimestamp (which is an unsigned 64-bit integer roughly
I had used several ways to do some simple integer arithmetic in BASH (3.2).
Is there a clean and simple method of formatting an integer which is a
I had a simple table: class test(Base): __tablename__ = 'test' id = Column(Integer, primary_key=True)
I have a console application written in c++. It simply reads an integer from
Edit: I have since found and published an efficient and elegant solution that transforms
I am having a problem figuring out how to simply return an integer from
Another collinear-points question. This one's twist is, I'm using integer arithmetic, and I'm looking
How do I add a simple integer to another integer in Javascript? I'm getting
I have a very simple iPhone app that requires a random integer from 1-100.

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.