JavaScript provides several bitwise operators that allow you to perform bitwise operations on integer values. These operators manipulate the binary representations of numbers at the bit level. Here are the bitwise operators available in JavaScript:
Operators | Name | Example |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
^ | Bitwise XOR | x ^ y |
~ | Bitwise NOT | ~x |
<< | Left shift | x << y |
>> | Sign-propagating right shift | x >> y |
>>> | Zero-fill right shift | x >>> y |
1. Bitwise AND (&
): Performs a bitwise AND operation on each corresponding pair of bits. If both bits are 1, the result is 1; otherwise, the result is 0.
let result = 5 & 3; // 0101 & 0011 = 0001 (1)
console.log(result); // Output: 1
2. Bitwise OR (|
): Performs a bitwise OR operation on each corresponding pair of bits. If at least one bit is 1, the result is 1; otherwise, the result is 0.
let result = 5 | 3; // 0101 | 0011 = 0111 (7)
console.log(result); // Output: 7
3. Bitwise XOR (^
): Performs a bitwise XOR (exclusive OR) operation on each corresponding pair of bits. If the two bits are different, the result is 1; otherwise, the result is 0.
let result = 5 ^ 3; // 0101 ^ 0011 = 0110 (6)
console.log(result); // Output: 6
4. Bitwise NOT (~
): Flips the bits of the operand. It converts each 0 to 1 and each 1 to 0. Note that it also changes the sign of the number due to the two’s complement representation of signed numbers.
let result = ~5; // ~0101 = 1010 (-6)
console.log(result); // Output: -6
5. Left Shift (<<
): Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Zeroes are shifted in from the right.
let result = 5 << 2; // 0101 << 2 = 10100 (20)
console.log(result); // Output: 20
6. Right Shift (>>
): Shifts the bits of the left operand to the right by the number of positions specified by the right operand. For non-negative numbers, zeroes are shifted in from the left. For negative numbers, ones are shifted in from the left, preserving the sign of the number.
let result = 5 >> 1; // 0101 >> 1 = 0010 (2)
console.log(result); // Output: 2
7. Unsigned Right Shift (>>>
): Shifts the bits of the left operand to the right by the number of positions specified by the right operand. Zeroes are shifted in from the left. The sign of the number is not preserved.
let result = -5 >>> 1; // 11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101 (2147483645)
console.log(result); // Output: 2147483645
These bitwise operators work on 32-bit signed integers in JavaScript. When using these operators, JavaScript internally converts the numbers to 32-bit binary representation, performs the operation, and returns the result.
JavaScript Bitwise Operators example
Simple example code that demonstrates the usage of JavaScript bitwise operators:
// Bitwise AND (&)
let num1 = 5; // Binary: 0101
let num2 = 3; // Binary: 0011
let resultAnd = num1 & num2;
console.log(resultAnd); // Output: 1
// Bitwise OR (|)
let resultOr = num1 | num2;
console.log(resultOr); // Output: 7
// Bitwise XOR (^)
let resultXor = num1 ^ num2;
console.log(resultXor); // Output: 6
// Bitwise NOT (~)
let resultNot = ~num1;
console.log(resultNot); // Output: -6
// Left Shift (<<)
let resultLeftShift = num1 << 2;
console.log(resultLeftShift); // Output: 20
// Right Shift (>>)
let resultRightShift = num1 >> 1;
console.log(resultRightShift); // Output: 2
// Unsigned Right Shift (>>>)
let negativeNum = -5; // Binary: 11111111111111111111111111111011
let resultUnsignedShift = negativeNum >>> 1;
console.log(resultUnsignedShift); // Output: 2147483645
Output:
Comment if you have any doubts or suggestions on this Js Operator topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version