Mind-Blowing JavaScript Bitwise Hacks You’ll Actually Use

Smith Kruz
3 min readJan 7, 2025

--

Introduction

When was the last time you used a bitwise operator in JavaScript? Maybe in an interview, or perhaps never? 🤔 For most developers, bitwise operators (&, |, ^, ~, etc.) feel like cryptic leftovers from a low-level programming era.

But here’s the kicker: they’re not just for show. These operators are lightning-fast and can perform powerful tasks when used creatively. In this post, we’ll uncover 18 mind-blowing bitwise tricks that you can actually use in your projects, from simple optimizations to real-world applications.

1. Check If a Number is Even or Odd

Tired of the % operator? Let’s go bitwise:

const isEven = (num) => (num & 1) === 0;
console.log(isEven(4)); // true
console.log(isEven(7)); // false

Why? Because the least significant bit of even numbers is always 0.

2. Swap Two Numbers Without a Temp Variable

A trick straight out of the wizarding world:

let a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a, b); // 10, 5

No temporary variables, just XOR magic!

3. Check Power of Two

Is your number a power of two? A single bitwise operation can tell you:

const isPowerOfTwo = (n) => n > 0 && (n & (n - 1)) === 0;
console.log(isPowerOfTwo(8)); // true
console.log(isPowerOfTwo(10)); // false

This works because the powers of two are exactly one bit set in their binary form.

4. Toggle Specific Bit

Need to flip a specific bit? Bitwise XOR (^) has your back:

const toggleBit = (num, bitPosition) => num ^ (1 << bitPosition);
console.log(toggleBit(5, 1)); // 7 (0101 -> 0111)

5. Count the Number of Set Bits

A.k.a. Hamming Weight, this is a must-have for coding challenges:

const countSetBits = (n) => {
let count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
};
console.log(countSetBits(9)); // 2 (1001 has two 1s)

6. Quick Boolean to Number Conversion

Turn true into 1 and false into 0 faster than a typecast:

const boolToNum = (bool) => +bool;
console.log(boolToNum(true)); // 1
console.log(boolToNum(false)); // 0

7. Bitmask for Permissions

Bitmasks are a classic use case for bitwise operators.

  • Read = 1
  • Write = 2
  • Execute = 4

Here’s how you manage permissions:

let permissions = 0;
permissions |= 1; // Grant Read
permissions |= 2; // Grant Write
console.log(permissions); // 3 (Read + Write)
permissions &= ~1; // Revoke Read
console.log(permissions); // 2 (Write only)

8. RGB Packing and Unpacking

Pack three color channels into a single integer:

const packRGB = (r, g, b) => (r << 16) | (g << 8) | b;
console.log(packRGB(255, 165, 0)); // 16753920

And unpack them just as easily:

const unpackRGB = (rgb) => ({
r: (rgb >> 16) & 0xff,
g: (rgb >> 8) & 0xff,
b: rgb & 0xff,
});
console.log(unpackRGB(16753920)); // { r: 255, g: 165, b: 0 }

9. Fast Modulo with Powers of 2

Replace % with & for faster modulo calculations:

const mod = (n, powerOfTwo) => n & (powerOfTwo - 1);
console.log(mod(77, 8)); // 5 (77 % 8)

10. Encrypt/Decrypt with XOR

Bitwise XOR is a simple but effective encryption method:

Encrypt:

const encrypt = (char, key) => char ^ key;
console.log(encrypt(65, 42)); // 107

Decrypt:

const decrypt = (encrypted, key) => encrypted ^ key;
console.log(decrypt(107, 42)); // 65

11. Detect Opposite Signs

Check if two numbers have opposite signs with a single XOR:

const isOppositeSign = (x, y) => (x ^ y) < 0;
console.log(isOppositeSign(5, -10)); // true
console.log(isOppositeSign(5, 10)); // false

12. Absolute Value Without Math.abs

const abs = (num) => (num ^ (num >> 31)) - (num >> 31);
console.log(abs(-42)); // 42
console.log(abs(42)); // 42

13. Clear or Isolate Bits

Clear the lowest set bit:

const clearLowestBit = (n) => n & (n - 1);
console.log(clearLowestBit(10)); // 8

Isolate the lowest set bit:

const isolateLowestBit = (n) => n & -n;
console.log(isolateLowestBit(10)); // 2

Conclusion

Bitwise operators are more than just esoteric tools — they’re a Swiss Army knife for efficient problem-solving in JavaScript. Whether you’re optimizing code, managing binary data, or tackling algorithm challenges, these hacks can make your work faster, cleaner, and more elegant.

So go ahead and add these tricks to your developer toolbox! 🚀

What’s your favorite bitwise trick? Let me know in the comments!

Let’s Connect!

Loved these hacks? Want to discuss more JavaScript wizardry or have questions about a specific trick? Let’s connect!

Feel free to share your own bitwise discoveries or just drop by to say hi. 🚀

--

--

Smith Kruz
Smith Kruz

Written by Smith Kruz

Experienced backend developer proficient in PHP, Node.js, Express, and MySQL. Dedicated to crafting efficient and secure web solutions. Let's innovate together!

No responses yet