Click here to Skip to main content
Licence 
First Posted 17 Nov 2007
Views 10,010
Downloads 58
Bookmarked 9 times

Bitwise operator(XOR)

By | 17 Nov 2007 | Article
XOR application by bitwise operator(XOR) or exchanging 2 variables values without use 3rd variable

Introduction

I was thinking that what is the purpose of the xor operator in the computer scince language then during playing with the xor operator i found a very amazing thing when i take the xor of the any two decimal binary i found a magecil number which tell me which bits are different in both numbers if they are different then i think by using this number i can exchange the two variables values without using any third variable how first i take the xor of two numbers and save that number in one of them now it is overwritte then i take the xor with the 2nd variable value by which its value become same as the 1st variable before overwrite then take the xor with the first variable with the 2nd updated variable value and get the old 2nd variable value and save in the first variable and finaly both variables value have been exchanged. One thing more the code of this is availabe in java,c++ and c#.

Using the code

//
// using System;
using System.Collections.Generic;
using System.Text;
namespace xorapp
{
    class application
    {
        private int bitsdifference(int var1, int var2)
        {
            return var1 ^ var2;//taking the difference according to the binay if any
        }
        public void exchange(ref int var1, ref int var2)
        {
            var1 = bitsdifference(var1, var2);//taking difference and overrwrite in the 1st variable
            var2 = var2 ^ var1;//by using getting old value of variable1 and saving in the variable 2
            var1 = var2 ^ var1;//by using the new value of variable2 and difference getting old value of variable2 and saving in the variable1
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            application obj = new application();
            int a = 6;
            int b = 1;
            Console.WriteLine("a is before exchanging:\t{0}",a);
            Console.WriteLine("b is before exchanging:\t{0}", b);
            obj.exchange(ref a, ref b);
            Console.WriteLine("a is after exchanging:\t{0}", a);
            Console.WriteLine("b is after exchanging:\t{0}", b);
            
        }
    }
}
//

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Abbas Ali Butt

Other

Pakistan Pakistan

Member

My name is Abbas Ali Butt. I am a student of Punjab University Information and Technology.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberjohny_d10:52 1 Apr '09  
GeneralEasier PinmemberSteve Hansen2:58 19 Nov '07  
AnswerRe: Easier PinmemberAbbas Ali Butt17:30 18 Dec '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Abbas Ali Butt
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid