Course Content
JavaScript Basics
0/7
JavaScript Statements
0/6
JavaScript Loops
0/5
JavaScript Operators
0/5
JavaScript Built-in Objects
0/4
JavaScript

Bitwise operators are used to perform binary logic with the bit patterns in integers. JavaScript supports several bitwise operators, including:

• `&` (Bitwise AND)
• `|` (Bitwise OR)
• `^` (Bitwise XOR)
• `~` (Bitwise NOT)
• `<<` (Left shift)
• `>>` (Sign-propagating right shift)
• `>>>` (Zero-fill right shift)

## How to Use Bitwise Operators

Here’s a brief explanation of how each operator works:

### Bitwise AND (`&`)

The `&` operator performs a bitwise AND operation.

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

### Bitwise OR (`|`)

The `|` operator performs a bitwise OR operation.

``````let x = 5;  // binary: 0101
let y = 3;  // binary: 0011
console.log(x | y); // 7 (binary: 0111)``````

### Bitwise XOR (`^`)

The `^` operator performs a bitwise XOR operation.

``````let x = 5;  // binary: 0101
let y = 3;  // binary: 0011
console.log(x ^ y); // 6 (binary: 0110)``````

### Bitwise NOT (`~`)

The `~` operator performs a bitwise NOT operation.

``````let x = 5;  // binary: 0101
console.log(~x); // -6 (binary: 1010)``````

### Left Shift (`<<`)

The `<<` operator shifts the bits of the number to the left and fills the right side with zeros.

``````let x = 5;  // binary: 0101
console.log(x << 1); // 10 (binary: 1010)``````

### Sign-propagating Right Shift (`>>`)

The `>>` operator shifts the bits of the number to the right. The left side is filled with the sign bit so that the sign of the number is preserved.

``````let x = 5;  // binary: 0101
console.log(x >> 1); // 2 (binary: 0010)``````

### Zero-fill Right Shift (`>>>`)

The `>>>` operator shifts the bits of the number to the right. The left side is filled with zeros.

``````let x = 5;  // binary: 0101
console.log(x >>> 1); // 2 (binary: 0010)``````

Using JavaScript’s bitwise operators is fundamental to performing bit-level operations in your code. They are essential tools in every developer’s toolkit.