JavaScript Bitwise Operators
Tags: javascript, webdev, bitwise
Published: 2022-12-31 09:14:26

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 JavaScript Bitwise Operators

## 1. 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.

 Below is an example of JavaScript Bitwise AND operation
`let x = 10;
let y = 6;
let result = x & y;
console.log(result); // 2
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_1)

## 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

Example of JavaScript Bitwise OR operation
`let x = 10;
let y = 6;
let result = x | y;
console.log(result); // 14
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_3)


## 3. 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.

Example of JavaScript Bitwise XOR operation
`let x = 10;
let y = 6;
let result = x ^ y;
console.log(result); // 12
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_5)


## 4. 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 performs a JavaScript Bitwise NOT operation on 6
`let x = 6;
let result = ~x;
console.log(result); // -7
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_7)


## 5. Bitwise Zero Fill Left Shift <<
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 << operator specifies the number to be shifted, and the operand after the << operator specifies the number of times to shift (number of 0 to be added).

The example below performs a JavaScript Bitwise Left Shift operation on 10 and 2
`let result = 10 << 2;
console.log(result); // 40
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_9)


## 6. Bitwise Sign Preserving Right Shift >>
The bitwise sign preserving right shift operation push in copies of the leftmost bits from the left and the rightmost bits fall of. The operand before the >> operator, specifies the number to be shifted, and the operand after the >> operator specifies the number of times to shift.

The example below performs a JavaScript Bitwise Sign Preserving Right Shift operation on -10 and 2
`let result = -10 >> 2;
console.log(result); // -3
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_11)

## 7. Bitwise Zero Fill Right Shift >>>
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 >>> operator specifies the number to be shifted, and the operand after the >>> operator specifies the number of times to shift (number of 0 to be added).

The example below performs a JavaScript Bitwise Zero Fill Right Shift operation on 10 and 2
`let result = 10 >>> 2;
console.log(result); // 2
`
[Run code on codetutorialz](https://codetutorialz.com/code-editor/javascript.php?file=js_bitwise_operators_13)


[Learn more about JavaScript Bitwise Operator here](https://codetutorialz.com/javascript/js_bitwise_operators.php)
[Complete JavaScript Tutorial](https://codetutorialz.com/javascript/)