• Home
  • About

szehau's weblog

Life, Internet, Software, Gadgets, Programming and Investments

  • life log
  • internet
  • programming
  • software
  • working holiday
  • gadget
  • project

Simple programming trick

January 23, 2006 01:16 by Sze Hau 2 Comments

How do we swap values of two integer variables?

Normally we will introduce a third variable as the temporay storage. For example in C/C++:

int a = 123456;
int b = 567890;
int temp;

temp = a;
a = b;
b = temp;

Ok the question now is: How do we swap values of two integer variables without third variable?

Here is the trick:

int a = 123456;
int b = 567890;

a ^= b;
b ^= a;
a ^= b;

Don’t believe? You can try it out yourself.
Note:

  • only for integer values (eg. 8, 16, 32, 64 bits integers)
  • ^ means eXclusive OR (XOR)

Explaination:
When a value is XOR with another value, the result is actually “storing” value of two values. When one of the original value is XOR again with the result value, you will get the another value. For example:

int a = 123;
int b = 456;
int resultOfXOR;

resultOfXOR = a ^ b; // "storing" 2 values

resultOfXOR ^ a; // return value of b
resultOfXOR ^ b; // return value of a

From the above code

int a = 123456;
int b = 567890;

a ^= b; // storing a^b into a. a becomes result of XOR
b ^= a; // get value of original a by resultOfXOR^b and store into b. Now b = 123456.
a ^= b; // get value of original b by resultOfXOR^b and store into a. Now a = 567890.

// Finally b = 123456 and a = 567890

That’s the beauty of XOR.

More from my site

  • Bai Tian Gong 2014Bai Tian Gong 2014
  • Upgraded to WordPress 2.2.1Upgraded to WordPress 2.2.1
  • Target SettingTarget Setting
  • Snow Wolf Lake Starts TonightSnow Wolf Lake Starts Tonight
  • Blogs Updates for February 2009Blogs Updates for February 2009
  • Happy Earth DayHappy Earth Day

Filed Under: programming

About Sze Hau

Geek. Love programming. Coffee addicted. Married with a child. Working towards financial freedom.

Comments

  1. Kuzco says

    February 26, 2006 08:58 at 8:58 am

    awesome!

    Reply
  2. mypapit says

    February 26, 2006 14:08 at 2:08 pm

    nice article, I’m not aware that we can do that with XOR

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

  • Facebook
  • Google+
  • Instagram
  • Twitter

More Posts

5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic

June 1, 2021 22:15 By Sze Hau Leave a Comment

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau Leave a Comment

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau Leave a Comment

Sponsored Links

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • Snippet IT Information technology news, ideas and experiences and opinions

szehau’s weblog

szehau is a software programmer that has strong interest in the world of programming and Internet. He is graduated in University of Malaya, Malaysia with a Bachelor’s degree in Software Engineering. He draws on his passsion for web programming to share with others how to build a successful websites.

Recent

  • 5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic
  • Troubleshooting A Flickering LED Downlight
  • Repair A Broken iPhone Lightning Cable
  • New Gadget – MacBook Pro with Retina Display (2015)
  • One Watt LED Night Light

Search

Tags

adsense advertisement baby blog blogtal Canon EOS 400D Chinese New Year Christchurch CNY DiGi electronic Facebook FBM Flickr Flickr Uploadr Google Google Maps Google Search Results Google Webmaster Central H1N1 income tax investment joke KLCI KLSE Lake Tekapo Maxis Maybank2u Motueka movie mutual funds New Zealand PC Fair 2009 Queenstown restaurant for dinner sharing stock trackback travel Twitter unit trust visa wordpress wordpress plugin working holiday

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in