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
<!DOCTYPEhtml>
<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">
<head>
<metacharset="utf-8"/>
<title>Swapping</title>
<linkrel="stylesheet"href="app.css"type="text/css"/>
<scriptsrc="app.js"></script>
</head>
<body>
<h2>Swapping using bitwise XOR in TypeScript</h2>
<divid="content"/>
</body>
</html> |
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: