JavaScript Bitwise Operators

In this chapter, we will learn about JavaScript bitwise operators. These operators perform operations on binary numbers at the bit level. We will cover:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)
  • Zero-fill Right Shift (>>>)

Bitwise AND (&)

The bitwise AND operator compares each bit of two numbers. If both bits are 1, the result is 1; otherwise, the result is 0.

Syntax

result = number1 & number2; 

Example

let a = 5; // 5 is 00000101 in binary let b = 3; // 3 is 00000011 in binary let result = a & b; // result is 00000001 in binary console.log(result); // Output: 1 

Bitwise OR (|)

The bitwise OR operator compares each bit of two numbers. If at least one bit is 1, the result is 1; otherwise, the result is 0.

Syntax

result = number1 | number2; 

Example

let a = 5; // 5 is 00000101 in binary let b = 3; // 3 is 00000011 in binary let result = a | b; // result is 00000111 in binary console.log(result); // Output: 7 

Bitwise XOR (^)

The bitwise XOR operator compares each bit of two numbers. If the bits are different, the result is 1; otherwise, the result is 0.

Syntax

result = number1 ^ number2; 

Example

let a = 5; // 5 is 00000101 in binary let b = 3; // 3 is 00000011 in binary let result = a ^ b; // result is 00000110 in binary console.log(result); // Output: 6 

Bitwise NOT (~)

The bitwise NOT operator inverts the bits of a number. Each 0 becomes 1 and each 1 becomes 0.

Syntax

result = ~number; 

Example

let a = 5; // 5 is 00000101 in binary let result = ~a; // result is 11111010 in binary (two's complement representation) console.log(result); // Output: -6 

Left Shift (<<)

The left shift operator shifts the bits of a number to the left by a specified number of positions. The rightmost bits are filled with 0.

Syntax

result = number << positions; 

Example

let a = 5; // 5 is 00000101 in binary let result = a << 2; // result is 00010100 in binary console.log(result); // Output: 20 

Right Shift (>>)

The right shift operator shifts the bits of a number to the right by a specified number of positions. The leftmost bits are filled with the sign bit (the bit farthest to the left), which means it preserves the sign of the number.

Syntax

result = number >> positions; 

Example

let a = 20; // 20 is 00010100 in binary let result = a >> 2; // result is 00000101 in binary console.log(result); // Output: 5 let b = -20; // -20 is 11101100 in binary (two's complement representation) result = b >> 2; // result is 11111011 in binary console.log(result); // Output: -5 

Zero-fill Right Shift (>>>)

The zero-fill right shift operator shifts the bits of a number to the right by a specified number of positions. The leftmost bits are filled with 0.

Syntax

result = number >>> positions; 

Example

let a = 20; // 20 is 00010100 in binary let result = a >>> 2; // result is 00000101 in binary console.log(result); // Output: 5 let b = -20; // -20 is 11101100 in binary (two's complement representation) result = b >>> 2; // result is 00111011 in binary console.log(result); // Output: 1073741819 

Conclusion

In this chapter, you learned about JavaScript bitwise operators, including bitwise AND, OR, XOR, NOT, left shift, right shift, and zero-fill right shift. These operators are useful for low-level operations and manipulating binary data.

Leave a Comment

Scroll to Top