DEV Community

Theophilus Kolawole
Theophilus Kolawole

Posted on

1

JavaScript Bitwise Operators

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

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

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

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

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

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

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

Learn more about JavaScript Bitwise Operator here
Complete JavaScript Tutorial

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay