DEV Community

dayu2333-jinyul
dayu2333-jinyul

Posted on • Originally published at bitwisecalc.com

3 Bitwise Operations I Use in Real Code (Not Just Interviews)

Bitwise operations have a reputation as interview trivia that nobody uses in production. Here's three I actually use regularly:

1. Flags with bitwise OR — Permission systems are cleaner with bitmasks. READ = 1, WRITE = 2, EXECUTE = 4. To grant read+write: userPerms = READ | WRITE = 3. To check: userPerms & WRITE returns truthy if the flag is set. This replaces 8 boolean columns with 1 integer.

2. Color manipulation — Hex colors are packed RGB. 0xFF5733 → R = (color >> 16) & 0xFF, G = (color >> 8) & 0xFF, B = color & 0xFF. Darken by 30%: shift each channel right by 30% with bitwise. No string parsing needed, runs 10x faster than hex-to-rgb-to-hsl-to-hex.

3. Even/odd checkn & 1 is faster than n % 2 because modulo involves division which is 10-20x slower on modern CPUs. For alternating row colors in a 10,000-row table, the difference is measurable.

bitwisecalc.com is a free bitwise calculator: AND, OR, XOR, NOT, left shift, right shift. Displays in binary, decimal, and hex simultaneously. Runs entirely in the browser — no server, no ads.

Check it out: bitwisecalc.com — instant bitwise math with no signup.

Top comments (0)