-->

JavaScript - Bitwise Operations Part 4: Bitwise NOT (~) and Left Shift (<<)

How NOT (~) Works

The NOT (~) operator inverts all bits, flipping 1 to 0 and 0 to 1.

Example 5: NOT Operation

console.log(~5); // Output: -6

Binary representation:

5  =  00000000000000000000000000000101

~5 =  11111111111111111111111111111010  (-6 in decimal)

In JavaScript, NOT flips all bits and returns the two's complement (negative equivalent).

How Left Shift (<<) Works

Left shifting moves bits left and fills with 0s, multiplying by 2.

Example 6: Left Shift Operation

console.log(5 << 2); // Output: 20

Binary representation:

5   =  00000000000000000000000000000101

<< 2 =  00000000000000000000000000010100  (20 in decimal)

Each left shift doubles the number.