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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:50:27+00:00 2026-05-24T06:50:27+00:00

a=b+(b=a)*0; This sentence can swap the value between a and b. I’ve tried it

  • 0
a=b+(b=a)*0;

This sentence can swap the value between a and b.
I’ve tried it with C# and it works.
But I just don’t konw how it works.
e.g.

a = 1, b = 2

I list the steps of it as below:

b = a -> a = 1, b = 1
b * 0 -> a = 1, b = 1
b + 0 -> a = 1, b = 1
a = b -> a = 1, b = 1 ?

But the value of b may be wrong.
Could anyone help me? It puzzles me a lot.

  • 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-05-24T06:50:28+00:00Added an answer on May 24, 2026 at 6:50 am
    a=b+(b=a)*0;
    

    That (b=a) in the middle is an assignment statement setting b to the current value of a with the side effect that its result is (the final value of) b. When that’s multiplied by 0, you get 0, but the assignment has still happened.

    The outer bit sets a to b plus the result of (b=a)*0 (which is zero). Because the original b has already been used to start setting a before the assignment in the (b=a) bit, the assignment has no effect on a. In other words, it’s an atomic a = b; b = a;, swapping the two values.

    C# evaluates things in a strict left to right order (unlike C and C++) to avoid a large class of subtle bugs. Note that the compiler can evaluate them in a different order but only if it can determine that it will have no effect (that’s not the case here).

    The snippet you posted works, but is also hideous and not a good idea, especially when you can just use the far more readable:

    int t = a; a = b; b = t;
    

    Tricks like what you see here, and the XOR swap trick, and Duff’s device have no place in decent, maintainable code. They’re usually a way for coders to show how clever they are but they usually just show how bad they are.

    If you ever see something like that in production code, I don’t think any jury in the world would convict you for hunting down the perpetrator and beating them to death with the rolled up program listing.

    You should write your code as if the next person that has to maintain it is a psychopath who knows where you live – in my case, you’d be half right – I have no idea where you live 🙂

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

Sidebar

Related Questions

I am reading the Java Hashmap documentation but I don't understand this sentence. Note
I have problem with translating this sentence: Running in-house systems means that you can
Can anyone explain me this sentence please? The sorting algorithm is a modified mergesort
This must be a simple problem but i can't figure out why it's not
I have been doing this by hand and I just can't do it anymore--
I've written some jquery to solve this problem. Although it works, I can't help
In Ruby Language - About we can find this sentence: In Ruby, everything is
In this sentence: myCommand.ExecuteReader(CommandBehavior.CloseConnection) does it close connection in case of exception?
C# It's possible to set to a variant this sentence {D , and receive
I'm using MySQL 5, and I need to do this sentence to get 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.