Bitwise Operators in Typescript

Bitwise operators in TypeScript manipulate individual bits (0s and 1s) within numeric data types. They are primarily used for low-level operations, flag manipulation, and specific tasks like masking and bit shifting.

List of Bitwise Operators

Here are the primary bitwise operators available in TypeScript:

  1. Bitwise AND (&): Performs bitwise AND operation on each corresponding bit of the operands, resulting in 1 only if both bits are 1.
  2. Bitwise OR (|): Performs bitwise OR operation on each corresponding bit of the operands, resulting in 1 if either bit is 1.
  3. Bitwise XOR (^): Performs bitwise XOR operation on each corresponding bit of the operands, resulting in 1 if the bits are different.
  4. Bitwise NOT (~): Inverts all the bits of the operand.
  5. Left shift operator (<<): Shifts all bits in the operand to the left by the specified number of positions.
  6. Right shift operator (>>): Shifts all bits in the operand to the right by the specified number of positions.
  7. Unsigned right shift operator (>>>): Shifts all bits in the operand to the right by the specified number of positions, treating the operand as an unsigned integer.

Bitwise AND (&)

The bitwise AND operator (&) performs a bitwise AND operation between corresponding bits of two operands.

let x: number = 5; // binary: 0101 let y: number = 3; // binary: 0011 let result: number = x & y; // Result: 1 (binary: 0001) console.log(result);

Here, the bitwise AND operation results in binary 0001, which is decimal 1.

Bitwise OR (|)

The bitwise OR operator (|) performs a bitwise OR operation between corresponding bits of two operands.

let a: number = 5; // binary: 0101 let b: number = 3; // binary: 0011 let result: number = a | b; // Result: 7 (binary: 0111) console.log(result);

The bitwise OR operation results in binary 0111, which is decimal 7.

Bitwise XOR (^)

The bitwise XOR operator (^) performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands.

let m: number = 5; // binary: 0101 let n: number = 3; // binary: 0011 let result: number = m ^ n; // Result: 6 (binary: 0110) console.log(result);

The bitwise XOR operation results in binary 0110, which is decimal 6.

Bitwise NOT (~)

The bitwise NOT operator (~) inverts the bits of its operand, changing each 0 to 1 and each 1 to 0.

let p: number = 5; // binary: 0101 let result: number = ~p; // Result: -6 (binary: 1010) console.log(result);

The bitwise NOT operation inverts the bits, resulting in binary 1010, which is decimal -6.

Left Shift (<<) and Right Shift (>>)

The left shift operator (<<) shifts the bits of the left operand to the left by a specified number of positions, filling in with zeros. The right shift operator (>>) shifts the bits to the right, preserving the sign bit for signed numbers.

let q: number = 5; // binary: 0101 let leftShifted: number = q << 1; // Result: 10 (binary: 1010) let rightShifted: number = q >> 1; // Result: 2 (binary: 0010) console.log(leftShifted, rightShifted);

Here, the left shift operation results in binary 1010 (decimal 10), and the right shift operation results in binary 0010 (decimal 2).

Applications

  1. Flag manipulation: Setting and clearing individual flags within a bitmask.
  2. Masking specific bits: Extracting specific parts of a data byte.
  3. Bit-level encryption/decryption: Performing basic encryption and decryption operations on binary data.
  4. Performance optimization: Replacing complex logic with bitwise operations for efficiency.

Understanding Binary Representation

To effectively utilize bitwise operators, understanding the binary representation of numbers is crucial. Visualizing the individual bits and how they are manipulated during operations significantly enhances your understanding.

Advanced Features

  1. Combining bitwise operators: Chaining multiple operators to achieve complex bit manipulations.
  2. Shifting by variables: Dynamically controlling the shift amount based on runtime values.
  3. Bitwise operations with other data types: Using bitwise operators with booleans and strings after converting them to bit representations.

Best Practices

  1. Use descriptive variable names and comments to explain bitwise operations.
  2. Ensure compatibility between data types involved in operations.
  3. Consider the impact of bit shifting on signed and unsigned integers.
  4. Use caution when dealing with large bit manipulations for performance considerations.

Conclusion

TypeScript bitwise operators, including AND (&), OR (|), XOR (^), NOT (~), left shift (&;t;<), and right shift (>>), manipulate the individual bits of binary representations of numeric values. These operators provide a way to perform low-level bit-level operations, making them useful for scenarios involving binary data manipulation, though care must be taken when working with signed numbers to avoid unexpected results.