# JavaScript Bitwise Operators

In JavaScript, numbers are stored as 64-bit floating points. Bitwise operators in JavaScript operate on 32-bit operands.

Before performing a bitwise operation, JavaScript converts 64-bit floating point numbers into 32-bit signed integers, it then converts the result back to 64-bit numbers.

### Examples of bitwise operators in JavaScript

| Operator | Name | Description |
| --- | --- | --- |
| & | AND | returns 1 if both bits are 1 |
|  |  | OR |
| ^ | XOR | returns 1 if only one of the two bits is 1 |
| ~ | NOT | Inverts all the bits (1 becomes 0, and 0 becomes 1) |
| &lt;&lt; | Zero-fill left shift | Shifts left by pushing zeros in from the right and letting the leftmost bits fall off |
| &gt;&gt; | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
| &gt;&gt;&gt; | Zero-fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |

*Note: The maximum integer that is representable through a 32-bit signed number is* ***2147483647*** *and the minimum is* ***\-2147483648***

## JavaScript Bitwise AND (&)

When the JavaScript Bitwise AND operation is performed on a pair of bits, it returns 1 if both bits are 1 otherwise it returns 0.

| Operand 1 | Operand 2 | Bitwise AND operation |
| --- | --- | --- |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

The example below shows the working of the bitwise AND operation between two integers 10 and 6

```plaintext
// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise AND operation of 10 and 6
    00001010
&   00000110
    --------
    00000010 = 2 (In decimal)
```

Example of JavaScript Bitwise AND operation

```javascript
let x = 10;
let y = 6;
let result = x & y;
console.log(result); // 2
```

## JavaScript Bitwise OR (|)

When the JavaScript Bitwise OR operation is performed on a pair of bits, it returns 1 if one of the bits is 1 otherwise it returns 0

| Operand 1 | Operand 2 | Bitwise OR Operation |
| --- | --- | --- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

The example below shows the working of the bitwise OR operation between two integers 10 and 6

```plaintext
// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise OR operation of 10 and 6
    00001010
|   00000110
    --------
    00001110 = 14 (In decimal)
```

Example of JavaScript Bitwise OR operation

```javascript
let x = 10;
let y = 6;
let result = x | y;
console.log(result); // 14
```

## JavaScript Bitwise XOR (^)

When the JavaScript Bitwise XOR operation is performed on a pair of bits, it returns 1 if the corresponding bits are different and returns 0 if they are the same.

| Operand 1 | Operand 2 | Bitwise XOR |
| --- | --- | --- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

The example below shows the working of the bitwise XOR operation between two integers 10 and 6

```plaintext
// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise XOR operation of 10 and 6
    00001010
^   00000110
    --------
    00001100 = 12 (In decimal)
```

Example of JavaScript Bitwise XOR operation

```javascript
let x = 10;
let y = 6;
let result = x ^ y;
console.log(result); // 12
```

## Bitwise NOT (~)

The JavaScript Bitwise NOT operator flips the bit (if the bit is 1 it becomes 0, and if the bit is 0, it becomes 1).

The example below shows the working of bitwise NOT operation on **6**

```plaintext
// From decimal to binary
6  = 00000000000000000000000000000110

// Bitwise NOT operation of 6
~   00000000000000000000000000000110
    --------------------------------
    11111111111111111111111111111001 = -7 (In decimal)
```

The example below performs a JavaScript Bitwise NOT operation on **6**

```javascript
let x = 6;
let result = ~x;
console.log(result); // -7
```

## Bitwise Zero Fill Left Shift &lt;&lt;

The bitwise zero fill left shift operation push in one or more zero bits from the right and the leftmost bits fall off. The operand before the &lt;&lt; operator specifies the number to be shifted, and the operand after the &lt;&lt; operator specifies the number of times to shift (number of 0 to be added).

The example below shows the working of bitwise zero fill left shift operation of 10 &lt;&lt; 2

```plaintext
// From decimal to binary
10 = 00000000000000000000000000001010

// Bitwise Left Shift operation of 10 << 2
<<  00000000000000000000000000101000 = 40 (In decimal)
```

The example below performs a JavaScript Bitwise Left Shift operation on **10** and **2**

```javascript
let result = 10 << 2;
console.log(result); // 40
```

## Bitwise Sign Preserving Right Shift &gt;&gt;

The bitwise sign preserving right shift operation push in copies of the leftmost bits from the left and the rightmost bits fall off. The operand before the **&gt;&gt;** operator, specify the number to be shifted, and the operand after the **&gt;&gt;** operator specifies the number of times to shift.

The example below shows the working of the bitwise Sign Preserving Right Shift operation of **\-10 &gt;&gt; 2**

```plaintext
// From decimal to binary
-10 = 11111111111111111111111111110110

// Bitwise Sign Preserving Right Shift operation of -10 << 2
<<  11111111111111111111111111111101 = -3 (In decimal)
```

The example below performs a JavaScript Bitwise Sign Preserving Right Shift operation on **\-10** and **2**

```javascript
let result = -10 >> 2;
console.log(result); // -3
```

## Bitwise Zero Fill Right Shift &gt;&gt;&gt;

The bitwise zero-fill right shift operation push in one or more zero bits from the left and the rightmost bits fall off. The operand before the **&gt;&gt;&gt;** operator specifies the number to be shifted, and the operand after the &gt;**&gt;&gt;** operator specifies the number of times to shift (number of 0 to be added).

The example below shows the working of the bitwise Zero Fill Right Shift operation of **10 &gt;&gt;&gt; 2**

```plaintext
// From decimal to binary
10 = 00000000000000000000000000001010

// Bitwise Left Shift operation of 10 >>> 2
<<<  00000000000000000000000000000010 = 2 (In decimal)
```

The example below performs a JavaScript Bitwise Zero Fill Right Shift operation on **10** and **2**

```javascript
let result = 10 >>> 2;
console.log(result); // 2
```

[Learn more about JavaScript](https://codetutorialz.com/javascript)
