Labs ICT
Pro Login

Bitwise

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);
Try it Yourself →

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);
Try it Yourself →

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}`);
Try it Yourself →

Bitwise Permissions Flags

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

let permissions = READ | WRITE;
console.log(permissions & READ);
console.log(permissions & EXECUTE);
Try it Yourself →