Bitwise Operators Detailed šŸŽÆ

beginner
15 min

Bitwise Operators Detailed šŸŽÆ

Welcome to the fascinating world of Bitwise Operators! Let's embark on a journey to understand this powerful tool in programming. We'll cover everything from the basics to advanced examples, making it easy for both beginners and intermediates to grasp.

Understanding Bitwise Operators šŸ“

Bitwise operators manipulate individual bits within an integer or binary number. They are crucial for low-level programming, optimizing code, and solving specific problems that require bit-level manipulation.

Binary Representation šŸ’”

To begin, let's understand binary numbers and their significance in bitwise operations. A binary number is a base-2 number system consisting of 0s and 1s.

Binary: 10101010 Decimal: 180 (calculated by adding the value of each bit)

Bitwise Operators Overview šŸ’”

There are six primary bitwise operators in JavaScript:

  1. & (Bitwise AND)
  2. | (Bitwise OR)
  3. ^ (Bitwise XOR)
  4. ~ (Bitwise NOT)
  5. << (Bitwise Left Shift)
  6. >> (Bitwise Right Shift)

Bitwise AND (&) šŸ’”

The & operator performs a bitwise AND operation, where both bits must be 1 for the result to be 1.

javascript
// Example 1: 5 (101) AND 3 (011) console.log(5 & 3); // Output: 1 (001)

Bitwise OR (|) šŸ’”

The | operator performs a bitwise OR operation, where either bit can be 1 for the result to be 1.

javascript
// Example 1: 5 (101) OR 3 (011) console.log(5 | 3); // Output: 7 (111)

Bitwise XOR (^) šŸ’”

The ^ operator performs a bitwise XOR operation, where the result is 1 if the bits are different and 0 if they are the same.

javascript
// Example 1: 5 (101) XOR 3 (011) console.log(5 ^ 3); // Output: 6 (110)

Bitwise NOT (~) šŸ’”

The ~ operator takes the bitwise complement of the number, changing all 1s to 0s and all 0s to 1s.

javascript
// Example 1: Bitwise NOT of 5 (101) console.log(~5); // Output: -6 (1101100)

Bitwise Left Shift (<<) šŸ’”

The << operator shifts the bits of a number to the left by the specified number of positions. The vacated bits are filled with zeros.

javascript
// Example 1: Shifting 5 (101) 2 positions to the left console.log(5 << 2); // Output: 20 (1010000)

Bitwise Right Shift (>>) šŸ’”

The >> operator shifts the bits of a number to the right by the specified number of positions. The vacated leftmost bits are filled with zeros (arithmetic right shift), and with sign bits (logical right shift).

javascript
// Example 1: Arithmetic right shift of 5 (101) 2 positions console.log(5 >> 2); // Output: 1 (01) // Example 2: Logical right shift of -5 (-1011) 2 positions console.log(-5 >>> 2); // Output: -13 (100011)

Practical Applications šŸ’”

Bitwise operators are essential for various real-world scenarios, such as:

  1. Checking whether a number is even or odd using the AND operator.
  2. Finding the highest set bit using the right shift operator.
  3. Swapping two numbers using XOR.
  4. Implementing efficient data compression and encryption algorithms.

Wrapping Up šŸ’”

Bitwise operators might seem complex initially, but with practice and understanding, they become an indispensable tool in your programming arsenal. Mastering bitwise operators will not only help you solve intricate problems but also make your code more efficient and secure.

Quick Quiz
Question 1 of 1

What does the `&` operator do in JavaScript?

Quick Quiz
Question 1 of 1

What is the result of the operation `5 & 3` in JavaScript?