TypeScript Bitwise Operators

Introduction

In this chapter, we will explore bitwise operators in TypeScript. Bitwise operators are used to perform operations at the binary level.

Table of Contents

  1. Definition
  2. Types of Bitwise Operators
    • Bitwise AND (&)
    • Bitwise OR (|)
    • Bitwise XOR (^)
    • Bitwise NOT (~)
    • Left Shift (<<)
    • Right Shift (>>)
    • Unsigned Right Shift (>>>)
  3. Examples and Output
  4. Conclusion

Definition

Bitwise operators in TypeScript are used to perform bit-level operations on binary representations of integers. These operators treat their operands as a set of 32 bits (zeroes and ones) and perform operations on those bits.

Types of Bitwise Operators

Bitwise AND (&)

Definition

The bitwise AND operator performs a logical AND operation on each pair of corresponding bits of two operands. The result is 1 if both corresponding bits are 1; otherwise, the result is 0.

Syntax

let result = operand1 & operand2;

Example

This example demonstrates the use of the bitwise AND operator.

let a: number = 5; // Binary: 0101
let b: number = 3; // Binary: 0011
let andResult = a & b;
console.log(andResult); // Output: 1 (Binary: 0001)

Output

1

Bitwise OR (|)

Definition

The bitwise OR operator performs a logical OR operation on each pair of corresponding bits of two operands. The result is 1 if at least one of the corresponding bits is 1; otherwise, the result is 0.

Syntax

let result = operand1 | operand2;

Example

This example demonstrates the use of the bitwise OR operator.

let a: number = 5; // Binary: 0101
let b: number = 3; // Binary: 0011
let orResult = a | b;
console.log(orResult); // Output: 7 (Binary: 0111)

Output

7

Bitwise XOR (^)

Definition

The bitwise XOR operator performs a logical XOR operation on each pair of corresponding bits of two operands. The result is 1 if exactly one of the corresponding bits is 1; otherwise, the result is 0.

Syntax

let result = operand1 ^ operand2;

Example

This example demonstrates the use of the bitwise XOR operator.

let a: number = 5; // Binary: 0101
let b: number = 3; // Binary: 0011
let xorResult = a ^ b;
console.log(xorResult); // Output: 6 (Binary: 0110)

Output

6

Bitwise NOT (~)

Definition

The bitwise NOT operator inverts the bits of its operand. Each bit is flipped, meaning 0 becomes 1 and 1 becomes 0.

Syntax

let result = ~operand;

Example

This example demonstrates the use of the bitwise NOT operator.

let a: number = 5; // Binary: 0101
let notResult = ~a;
console.log(notResult); // Output: -6 (Binary: 1010, but considering the sign bit)

Output

-6

Left Shift (<<)

Definition

The left shift operator shifts the bits of its operand to the left by the specified number of positions. Zero bits are shifted in from the right.

Syntax

let result = operand << numberOfPositions;

Example

This example demonstrates the use of the left shift operator.

let a: number = 5; // Binary: 0101
let leftShiftResult = a << 1;
console.log(leftShiftResult); // Output: 10 (Binary: 1010)

Output

10

Right Shift (>>)

Definition

The right shift operator shifts the bits of its operand to the right by the specified number of positions. The sign bit is used to fill the leftmost bits.

Syntax

let result = operand >> numberOfPositions;

Example

This example demonstrates the use of the right shift operator.

let a: number = 5; // Binary: 0101
let rightShiftResult = a >> 1;
console.log(rightShiftResult); // Output: 2 (Binary: 0010)

Output

2

Unsigned Right Shift (>>>)

Definition

The unsigned right shift operator shifts the bits of its operand to the right by the specified number of positions. Zero bits are shifted in from the left.

Syntax

let result = operand >>> numberOfPositions;

Example

This example demonstrates the use of the unsigned right shift operator.

let a: number = -5; // Binary: 11111111111111111111111111111011 (in 32-bit signed integer representation)
let unsignedRightShiftResult = a >>> 1;
console.log(unsignedRightShiftResult); // Output: 2147483645 (Binary: 01111111111111111111111111111101)

Output

2147483645

Examples and Output

Example 1: Using Bitwise Operators

In this example, we will use all the bitwise operators to perform different operations.

TypeScript Code (src/index.ts)

let a: number = 5; // Binary: 0101
let b: number = 3; // Binary: 0011

// Bitwise AND
let andResult = a & b;
console.log("Bitwise AND: " + andResult); // Output: 1

// Bitwise OR
let orResult = a | b;
console.log("Bitwise OR: " + orResult); // Output: 7

// Bitwise XOR
let xorResult = a ^ b;
console.log("Bitwise XOR: " + xorResult); // Output: 6

// Bitwise NOT
let notResult = ~a;
console.log("Bitwise NOT: " + notResult); // Output: -6

// Left Shift
let leftShiftResult = a << 1;
console.log("Left Shift: " + leftShiftResult); // Output: 10

// Right Shift
let rightShiftResult = a >> 1;
console.log("Right Shift: " + rightShiftResult); // Output: 2

// Unsigned Right Shift
let unsignedRightShiftResult = -5 >>> 1;
console.log("Unsigned Right Shift: " + unsignedRightShiftResult); // Output: 2147483645

Compiling to JavaScript

To compile the TypeScript code to JavaScript, run the TypeScript compiler:

tsc src/index.ts

Output in JavaScript (src/index.js)

var a = 5; // Binary: 0101
var b = 3; // Binary: 0011

// Bitwise AND
var andResult = a & b;
console.log("Bitwise AND: " + andResult); // Output: 1

// Bitwise OR
var orResult = a | b;
console.log("Bitwise OR: " + orResult); // Output: 7

// Bitwise XOR
var xorResult = a ^ b;
console.log("Bitwise XOR: " + xorResult); // Output: 6

// Bitwise NOT
var notResult = ~a;
console.log("Bitwise NOT: " + notResult); // Output: -6

// Left Shift
var leftShiftResult = a << 1;
console.log("Left Shift: " + leftShiftResult); // Output: 10

// Right Shift
var rightShiftResult = a >> 1;
console.log("Right Shift: " + rightShiftResult); // Output: 2

// Unsigned Right Shift
var unsignedRightShiftResult = -5 >>> 1;
console.log("Unsigned Right Shift: " + unsignedRightShiftResult); // Output: 2147483645

Running the JavaScript

To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:

node src/index.js

Conclusion

In this chapter, we covered bitwise operators in TypeScript, including bitwise AND, bitwise OR, bitwise XOR, bitwise NOT, left shift, right shift, and unsigned right shift. We provided examples with their outputs to illustrate how these operators work in TypeScript.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare