JS Bitwise operation

JavaScript Bitwise Operators and Operations

In JavaScript, a number is typically stored as a 64-bit floating-point number. However, bitwise operations are performed on a 32-bit binary number.

To perform a bitwise operation, JavaScript converts the number into a 32-bit binary number (signed), performs the operation at the binary level, and then converts the result back into a standard 64-bit JavaScript number.

Understanding JavaScript bitwise operators is crucial for low-level programming, performance optimizations, and manipulating binary data.


List of JavaScript Bitwise Operators

Here is a detailed explanation of each JavaScript bitwise operator, complete with syntax, examples, and truth tables.

1. Bitwise AND Operator (&)

The Bitwise AND operator is a binary operator (i.e., it accepts two operands). The Bitwise AND (&) returns 1 if both the corresponding bits of the operands are set (i.e., 1). It returns 0 in any other case.

Bitwise AND Example

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

Truth Table for Bitwise AND:

A B OUTPUT ( A & B )
0 0 0
0 1 0
1 0 0
1 1 1

2. Bitwise OR Operator (|)

The Bitwise OR operator is a binary operator. Bitwise OR (|) returns 1 if any of the corresponding bits of the operands is set (i.e., 1), and returns 0 only if both bits are 0.

Bitwise OR Example

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

Truth Table for Bitwise OR:

A B OUTPUT ( A | B )
0 0 0
0 1 1
1 0 1
1 1 1

3. Bitwise XOR Operator (^)

The Bitwise XOR (Exclusive OR) operator is a binary operator. Bitwise XOR (^) returns 1 if both the operands are different (one is 1 and the other is 0). It returns 0 if both bits are the same.

Bitwise XOR Example

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

Truth Table for Bitwise XOR:

A B OUTPUT ( A ^ B )
0 0 0
0 1 1
1 0 1
1 1 0

4. Bitwise NOT Operator (~)

The Bitwise NOT operator is a unary operator (i.e., it accepts a single operand). Bitwise NOT (~) simply flips the bits of its operand: 0 becomes 1, and 1 becomes 0.

Bitwise NOT Example

console.log(~10);  // Output: -11
console.log(~-10); // Output: 9

Truth Table for Bitwise NOT:

A OUTPUT ( ~A )
0 1
1 0

5. Left Shift Operator (<<)

The Left Shift (<<) is a binary operator. The first operand specifies the number, and the second operand specifies the number of bits to shift. Each bit is shifted towards the left, and 0 bits are added from the right. The excess bits shifted off to the left are discarded.

Left Shift Example

let a = 4; 
console.log(a << 1); // Output: 8
console.log(a << 4); // Output: 64

Example Breakdown:

Variable Binary Representation Decimal
A 00000000000000000000000000000110 6
B 00000000000000000000000000000001 1
OUTPUT (A << B) 00000000000000000000000000001100 12

(Note: Shifting left by 1 is mathematically equivalent to multiplying the number by 2.)


6. Sign Propagating Right Shift Operator (>>)

The Sign Propagating Right Shift (>>) is a binary operator. Each bit is shifted towards the right, and the overflowing bits on the right are discarded.

This is known as "Sign Propagating" because the new bits added from the left depend upon the sign of the original number (i.e., 0 is added if the number is positive, and 1 is added if the number is negative).

Sign Propagating Right Shift Example

let a = 4; 
let b = -32; 
console.log(a >> 1); // Output: 2
console.log(b >> 4); // Output: -2

Example Breakdown:

Variable Binary Representation Decimal
A 00000000000000000000000000000110 6
B 00000000000000000000000000000001 1
OUTPUT (A >> B) 00000000000000000000000000000011 3

7. Zero Fill Right Shift Operator (>>>)

The Zero Fill Right Shift (>>>) is a binary operator. Each bit is shifted towards the right, and the overflowing bits are discarded. Unlike the sign-propagating shift, a 0 bit is always added from the left regardless of the sign. Therefore, it is called the zero fill right shift.

Zero Fill Right Shift Example

let a = 4; 
let b = -1; 
console.log(a >>> 1); // Output: 2
console.log(b >>> 4); // Output: 268435455

Example Breakdown:

Variable Binary Representation Decimal
A 00000000000000000000000000000110 6
B 00000000000000000000000000000001 1
OUTPUT (A >>> B) 00000000000000000000000000000011 3

Summary of JavaScript Bitwise Operators

Below is a quick reference cheat sheet for the Bitwise Operators available in JavaScript:

OPERATOR NAME USAGE DESCRIPTION
Bitwise AND (&) a & b Returns true (1) if both operands are true (1)
Bitwise OR (` `) `a
Bitwise XOR (^) a ^ b Returns true (1) if both operands are different
Bitwise NOT (~) ~ a Flips the value of the operand's bits
Bitwise Left Shift (<<) a << b Shifts the bits toward the left
Bitwise Right Shift (>>) a >> b Shifts the bits towards the right (preserves sign)
Zero Fill Right Shift (>>>) a >>> b Shifts the bits towards the right but adds 0 from left

Recommended Links

Exercise

?

Which bitwise operator shifts bits to the right and always fills the new left bits with zeros, regardless of the sign?