JavaScript Bitwise

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Bitwise operators work on 32-bit integer representations of numbers: &, |, ^, ~, and shifts <<, >>, >>>.

JavaScript Syntax

JavaScript
console.log((5 & 3).toString(2));
console.log((5 | 2).toString(2));
console.log(1 << 3);
Try it Editor

How It Works

  1. &. Bits set in both.
  2. |. Bits set in either.
  3. <<. Shift left multiplies by powers of two.

Examples

Example 1 — Flags with &

Test a bit flag.

JavaScript
const READ = 1, WRITE = 2;
const perms = READ | WRITE;
console.log((perms & WRITE) !== 0);
Try it Editor

How It Works

  1. OR combines flags.
  2. AND tests one flag.

Example 2 — XOR toggle

^ flips bits.

JavaScript
console.log(5 ^ 1);
Try it Editor

How It Works

  1. Flips the lowest bit of 5.

Example 3 — >>> zero-fill

Unsigned right shift.

JavaScript
console.log((-1 >>> 0) > 0);
Try it Editor

How It Works

  1. Treats the value as unsigned 32-bit.

Frequently Asked Questions

Operands are converted to 32-bit integers first.
It inverts bits. For "not found" tricks with indexOf, prefer clearer code.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful