• JavaScript Basics

  • Operators in JavaScript

  • Conditional Statements in JavaScript

  • JavaScript Strings

  • JavaScript Arrays

  • JavaScript Loop

  • JavaScript Functions

  • Conclusion

JavaScript Bitwise Operators

JavaScript bitwise operators allow you to perform operations on the binary representations of numbers. They work on 32-bit numbers at the binary level, manipulating bits directly. These operators are useful for low-level operations, such as optimizing performance or working with systems that require bit-level manipulation.

How Bitwise Operators Work

Before applying bitwise operations, JavaScript converts numbers into 32-bit binary. After performing the operation, the result is converted back to a standard JavaScript number.

1. & (AND)

The & operator compares each bit of two numbers and returns 1 only if both bits are 1.

Example:

javascript
1let result = 5 & 1;  // Binary: 0101 & 0001
2console.log(result); // Output: 1

Explanation:

  • 0101 (binary for 5)
  • 0001 (binary for 1)
  • 0101 & 0001 = 0001 (binary for 1)

2. | (OR)

The | operator compares each bit of two numbers and returns 1 if at least one of the bits is 1.

Example:

javascript
1let result = 5 | 1;  // Binary: 0101 | 0001
2console.log(result); // Output: 5

Explanation:

  • 0101 (binary for 5)
  • 0001 (binary for 1)
  • 0101 | 0001 = 0101 (binary for 5)

3. ~ (NOT)

The ~ operator inverts all the bits of the number (turns 0s to 1s and vice versa).

Example:

javascript
1let result = ~5;  // Binary: ~0101
2console.log(result); // Output: -6

Explanation:

  • 0101 (binary for 5)
  • ~0101 = 1010 (binary for -6 in two's complement representation)

4. ^ (XOR)

The ^ operator compares each bit of two numbers and returns 1 if the corresponding bits are different.

Example:

javascript
1let result = 5 ^ 1;  // Binary: 0101 ^ 0001
2console.log(result); // Output: 4

Explanation:

  • 0101 (binary for 5)
  • 0001 (binary for 1)
  • 0101 ^ 0001 = 0100 (binary for 4)

5. << (Left Shift)

The << operator shifts the bits of the number to the left by the specified number of positions, filling the vacated bits with 0.

Example:

javascript
1let result = 5 << 1;  // Binary: 0101 << 1
2console.log(result); // Output: 10

Explanation:

  • 0101 (binary for 5)
  • 1010 (binary for 10 after shifting left by 1)

6. >> (Right Shift)

The >> operator shifts the bits of the number to the right by the specified number of positions, filling the leftmost bits with the sign bit (0 for positive numbers, 1 for negative).

Example:

javascript
1let result = 5 >> 1;  // Binary: 0101 >> 1
2console.log(result); // Output: 2

Explanation:

  • 0101 (binary for 5)
  • 0010 (binary for 2 after shifting right by 1)

7. >>> (Unsigned Right Shift)

The >>> operator shifts the bits to the right, just like >>, but fills the leftmost bits with 0 regardless of the sign.

Example:

javascript
1let result = 5 >>> 1;  // Binary: 0101 >>> 1
2console.log(result); // Output: 2

Explanation:

  • 0101 (binary for 5)
  • 0010 (binary for 2 after unsigned right shift by 1)

Use Cases of Bitwise Operators

  • Low-Level Programming: Useful in tasks like bit masking, setting specific bits, or clearing bits.
  • Performance Optimization: Sometimes bitwise operations can offer faster alternatives to certain arithmetic operations.
  • Graphics and Game Development: Often used in manipulating pixel data and optimizing graphical calculations.

Frequently Asked Questions