Labs ICT
⭐ Pro Login

Bitwise

1 min read | JavaScript Tutorial
⭐

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Bitwise Operators

Bitwise operators work on the binary representation of numbers. They're used in low-level programming, flags, permissions, and performance-critical code. You won't use them every day, but when you need them, they're indispensable.

Bitwise AND (&) and OR (|)

console.log(5 & 3);
console.log(5 | 3);

5 in binary is 0101, 3 is 0011. AND gives 0001 (1), OR gives 0111 (7).

Bitwise XOR (^) and NOT (~)

console.log(5 ^ 3);
console.log(~5);

XOR flips bits where operands differ. NOT inverts all bits.

Bitwise Color Extraction

let color = 0x66CCFF;
let red = (color >> 16) & 0xFF;
let green = (color >> 8) & 0xFF;
let blue = color & 0xFF;
console.log(`R:${red} G:${green} B:${blue}`);

Bitwise Permissions Flags

const READ = 1;
const WRITE = 2;
const EXECUTE = 4;

let permissions = READ | WRITE;
console.log(permissions & READ);
console.log(permissions & EXECUTE);