JS Bitwise

JavaScript Bitwise Operators

JavaScript bitwise operators treat their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. They perform operations on the binary representations, but they return standard JavaScript numerical values.

Assume variable A holds 2 (binary 0010) and variable B holds 3 (binary 0011):

Operator Name Description Example
& Bitwise AND Performs a Boolean AND operation on each bit of its integer arguments. (A & B) is 2
| Bitwise OR Performs a Boolean OR operation on each bit of its integer arguments. (A | B) is 3
^ Bitwise XOR Performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. (A ^ B) is 1
~ Bitwise NOT A unary operator that operates by reversing all the bits in the operand. (~B) is -4
<< Left Shift Moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. (A << 1) is 4
>> Right Shift Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. (A >> 1) is 1
>>> Right Shift with Zero This operator is just like the >> operator, except that the bits shifted in on the left are always zero. (A >>> 1) is 1

1. Bitwise AND (&)

The bitwise AND (&) operator returns a 1 in each bit position for which the corresponding bits of both operands are 1.

Bitwise AND Syntax

a & b

Bitwise AND (&) Example

let a = 2; // Binary: 0010
let b = 3; // Binary: 0011

let result = a & b; // Binary: 0010 console.log("(a & b) => " + result); // Outputs: 2


2. Bitwise OR (|)

The bitwise OR (|) operator returns a 1 in each bit position for which the corresponding bits of either or both operands are 1.

Bitwise OR Syntax

a | b

Bitwise OR (|) Example

let a = 2; // Binary: 0010
let b = 3; // Binary: 0011

let result = a | b; // Binary: 0011 console.log("(a | b) => " + result); // Outputs: 3


3. Bitwise XOR (^)

The bitwise XOR (^) operator returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1.

Bitwise XOR Syntax

a ^ b

Bitwise XOR (^) Example

let a = 2; // Binary: 0010
let b = 3; // Binary: 0011

let result = a ^ b; // Binary: 0001 console.log("(a ^ b) => " + result); // Outputs: 1


4. Bitwise NOT (~)

The bitwise NOT (~) operator reverses the bits of its operand. It transforms 0 to 1 and 1 to 0.

Bitwise NOT Syntax

~a

Bitwise NOT (~) Example

let b = 3; // Binary: 00000000000000000000000000000011

let result = b; // Binary: 11111111111111111111111111111100 console.log("(b) => " + result); // Outputs: -4


5. Left Shift (<<)

The left shift (<<) operator moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

Left Shift Syntax

a << b

Left Shift (<<) Example

let a = 2; // Binary: 0010

let result = a << 1; // Binary: 0100 console.log("(a << 1) => " + result); // Outputs: 4


6. Right Shift (>>)

The binary right shift (>>) operator moves the left operand’s value right by the number of bits specified by the right operand. The sign bit is preserved in this operation.

Right Shift Syntax

a >> b

Right Shift (>>) Example

let a = 2; // Binary: 0010

let result = a >> 1; // Binary: 0001 console.log("(a >> 1) => " + result); // Outputs: 1


7. Right Shift with Zero (>>>)

The right shift with zero (>>>) operator is just like the >> operator, except that the bits shifted in on the left are always zero. This is also called Unsigned Right Shift.

Unsigned Right Shift Syntax

a >>> b

Right Shift with Zero (>>>) Example

let a = 2; // Binary: 0010

let result = a >>> 1; // Binary: 0001 console.log("(a >>> 1) => " + result); // Outputs: 1


Exercise

?

Which bitwise operator reverses all the bits in the operand?