ARTICLE

Swapping Using Bitwise XOR in TypeScript

Posted by | November 22, 2012
In this article I will explain swapping using bitwise XOR in TypeScript with an example.

Swapping Using Bitwise XOR in TypeScript

The bitwise XOR operator is indicated by a caret ( ^ ) and it works directly on the binary digits of a number. A TypeScript program can swap two numbers with and without using a third variable in a function and using the bitwise XOR operator.

We use swapping while sorting. Swapping can re-arrange numbers in a specified order either in descending order or in ascending order.

Example

The following example shows how to use swapping using bitwise XOR in TypeScript. For example if in your TypeScript program you have two variables x and y where x = 2 and y = 9, then

before swapping:

x = 4

y = 5

After swapping:

x = 9

y = 2

To see how it works use the following instructions.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is subsequently opened. Provide a name for your application like "Swapping", then click on the Ok button.

Step 2

After Step 1 your project has been created. Solution Explorer, which is at the right side of Visual Studio, contains the following:

• css: This is the design style sheet file. Styles define how to display HTML elements.
• ts: This is the TypeScript file. All classes and functionality are defined in this file.
• js: This is the JavaScript file. This file is created from the TypeScript file.

Step 3

swapping.ts

 class Swapping {     swap()     {        var x, y;        x = parseInt(prompt("Enter the First Number"));        y = parseInt(prompt("Enter the Second Number"));               var span = document.createElement("span");         span.style.color = "Green";        span.innerText = "Before Swapping \n"+"First number x ->" + x +"\n"+"Second number y ->"+y+"\n";        document.body.appendChild(span);                      x = x ^ y;        y = x ^ y;        x = x ^ y;              var span = document.createElement("span");        span.style.color = "Blue";        span.innerText = "After Swapping \n"+"First number x ->" + x +"\n"+"Second number y ->"+y+"\n";        document.body.appendChild(span);               }      } window.onload = () => {     var change = new Swapping();     change.swap(); }

Demo.html

           Swapping

Swapping using bitwise XOR in TypeScript

app.js

 var Swapping = (function () {     function Swapping() { }     Swapping.prototype.swap = function () {         var x;         var y;           x = parseInt(prompt("Enter the First Number"));         y = parseInt(prompt("Enter the Second Number"));         var span = document.createElement("span");         span.style.color = "Green";         span.innerText = "Before Swapping \n" + "First number x ->" + x + "\n" + "Second number y ->" + y + "\n";         document.body.appendChild(span);         x = x ^ y;         y = x ^ y;         x = x ^ y;         var span = document.createElement("span");         span.style.color = "Blue";         span.innerText = "After Swapping \n" + "First number x ->" + x + "\n" + "Second number y ->" + y + "\n";         document.body.appendChild(span);     };     return Swapping; })(); window.onload = function () {     var change = new Swapping();     change.swap(); };

Output 1

Enter the first number:

Output 2

Enter the second number:

Then click on the ok button.

Output 3

After the swapping:

 This Feature is Sponsored By .NET Diagramming framework designed for creating advanced solutions