Example 1 — Flags with &
Test a bit flag.
JavaScript
const READ = 1, WRITE = 2;
const perms = READ | WRITE;
console.log((perms & WRITE) !== 0);How It Works
- OR combines flags.
- AND tests one flag.
Bitwise operators work on 32-bit integer representations of numbers: &, |, ^, ~, and shifts <<, >>, >>>.
console.log((5 & 3).toString(2));
console.log((5 | 2).toString(2));
console.log(1 << 3);Test a bit flag.
const READ = 1, WRITE = 2;
const perms = READ | WRITE;
console.log((perms & WRITE) !== 0);^ flips bits.
console.log(5 ^ 1);Unsigned right shift.
console.log((-1 >>> 0) > 0);CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful