Bitwise Operators in TypeScript
~ (Bitwise Negation) operator
The Bitwise negation operator (~) will invert it's single operand, in other words, the negation operator performs a bitwise complement on its single integer operand.
| Initial value1 (A) | Initial value2 (B) | Resulting value1 (~A) | Initial value2 (~B) |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 0 | 0 |
Example
var a=3
var c=~a;
var c=~a;
Then the result in c is -4.
& (Bitwise And) operator
| Initial value1 (A) | Initial value2 (B) | |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example
var a=3,b=4;
var c=a&b;
var c=a&b;
Then the result in c is 0 because the above example is evaluated that way.

| (Bitwise OR) operator
| Initial value1 (A) | Initial value2 (B) | A|B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Example
var a=3,b=4;
var c=a|b;
var c=a|b;
Then the result c is 7 because the above example is evaluated that way.

^ (Bitwise XOR) operator
| Initial value1 (A) | Initial value2 (B) | A^B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example
var a=2,b=3;
var c=a^b;
The result c is 1 because the above example is evaluated that way.
The value of a is 2 (0010) and the value of b is 3, in other words, 0011' now 0010 | 0011 is 1:
The result is 0001, in other words, decimal One (1).
The Bitwise LeftShift (<<) operator shifts bits from the low bits to the high bits by the specified number of bit positions.
Example
var a=3,b=4;
var c=a<<b;
The result c is 48.
The Bitwise LeftShift (<<) operator shifts the bits from the high bits to the low bits by the specified number of bit positions.
Example
var a=3,b=4;
var c=a>>b;
The result of c is 0.
The following examples show you how to use bitwise operators in TypeScript. Use the following to create a program using bitwise operators.
Step 1
Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is opened. Provide the name of your application like "BitWiseOperators", then click on the Ok button.
Step 2
After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, CSS file, and HTML files.
Step 3
The code of the Bitwise operator's program.
var c=a^b;

<< (Bitwise LeftShift) operator
var c=a<<b;
>> (Bitwise RightShift) operator
var c=a>>b;
BitWiseOperators .ts
- class BitWiseOperators {
- MyFunction()
- {
- var a = 3,
- b = 4;
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a & b; //BitWise And operator
- y.innerText = "BitWise And operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = a | b; //BitWise OR operator
- y.innerText = "BitWise OR operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a ^ b; //BitWise XOR operator
- y.innerText = "BitWise XOR operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = a << b; //BitWise LeftShift operator
- y.innerText = "BitWise LeftShift operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a >> b; //BitWise RightShift operator
- y.innerText = "BitWise RightShift operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = ~a; //BitWise Negation operator
- y.innerText = "BitWise Negation operator res=" + c;
- document.body.appendChild(y);
- }
- }
- window.onload = () => {
- var call = new BitWiseOperators();
- call.MyFunction();
- }
default.html
- <!DOCTYPEhtml>
- <htmllang="en"
- xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metacharset="utf-8"/>
- <title>TypeScript HTML App</title>
- <linkrel="stylesheet"href="app.css"type="text/css"/>
- <scriptsrc="app.js">
- </script>
- </head>
- <body>
- <h1>Bitwise Operators Example</h1>
- <divid="content"/>
- </body>
- </html>
app.js
- var BitWiseOperators = (function() {
- function BitWiseOperators() {}
- BitWiseOperators.prototype.MyFunction = function() {
- var a = 3;
- var b = 4;
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a & b;
- y.innerText = "BitWise And operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = a | b;
- y.innerText = "BitWise OR operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a ^ b;
- y.innerText = "BitWise XOR operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = a << b;
- y.innerText = "BitWise LeftShift operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- y.style.color = "red";
- var c = a >> b;
- y.innerText = "BitWise RightShift operator res=" + c + "\n";
- document.body.appendChild(y);
- var y = document.createElement("y");
- var c = ~a;
- y.innerText = "BitWise Negation operator res=" + c;
- document.body.appendChild(y);
- };
- return BitWiseOperators;
- })();
- window.onload = function() {
- var call = new BitWiseOperators();
- call.MyFunction();
- };
Step 4
Output


Comments
Join the conversation! Your thoughts help the community grow.