Binary Operations in Programming: A Practical Guide
Bitwise operators let you inspect and change individual bits in an integer. This guide uses masks, permissions, packed colors, and shifts to show where they are useful in code. The JavaScript examples use 32-bit integer operations on Number values.
Why Binary Operations Matter
Even in high-level languages like JavaScript, Python, or Java, binary operations power critical functionality:
- Graphics: Color manipulation, transparency, pixel operations
- Networking: IP addresses, subnet masks, protocol flags
- Security: Cryptography, hashing, permission systems
- Game Development: Collision detection, state management
- Embedded Systems: Hardware register manipulation
- Performance: Fast math operations (multiply/divide by powers of 2)
Core Bitwise Operators
AND (&)
Returns 1 only if both bits are 1. Used for masking (extracting specific bits).
1111 0000 (&)
--------
1010 0000
Use case: Checking if specific flags are set
OR (|)
Returns 1 if at least one bit is 1. Used for setting bits.
0000 1111 (|)
--------
1010 1111
Use case: Combining flags/permissions
XOR (^)
Returns 1 when the corresponding bits differ. Use it to toggle selected bits or compare two bit patterns.
1111 0000 (^)
--------
0101 1100
Use case: Toggling flags. XOR with a fixed key alone is not secure encryption.
NOT (~)
Inverts all bits (0 becomes 1, 1 becomes 0).
--------
0101 0011
Use case: Creating masks, bitwise complement
Shift Operators
Left Shift (<<)
Shifts bits left and fills the low positions with zeros. In JavaScript Number operations, this uses a signed 32-bit value and discards bits shifted past that width.
= 0010 1000 (40)
Right Shift (>>)
Shifts bits right while copying the sign bit into the high positions. For a non-negative integer this acts like division by a power of two, rounded down.
= 0000 1010 (10)
Real-World Programming Examples
1. Permission System (Linux-style)
// Define permission constants
const READ = 4; // 100 in binary
const WRITE = 2; // 010 in binary
const EXECUTE = 1; // 001 in binary
// Grant permissions
let userPerms = READ | WRITE; // 110 (6)
// Check permissions
if (userPerms & READ) {
console.log("Can read!");
}
// Add permission
userPerms |= EXECUTE; // Now 111 (7)
// Remove permission
userPerms &= ~WRITE; // Now 101 (5)2. Color Manipulation
// Extract RGB components from hex color
const color = 0xFF5733; // Orange-red
const red = (color >> 16) & 0xFF; // 255
const green = (color >> 8) & 0xFF; // 87
const blue = color & 0xFF; // 51
// Make color 50% darker
const darker = ((red >> 1) << 16) |
((green >> 1) << 8) |
(blue >> 1);3. Checking if Number is Power of 2
// Clever bit trick: powers of 2 have exactly one bit set
// 8 = 1000, 7 = 0111, 8 & 7 = 0
// 16 = 10000, 15 = 01111, 16 & 15 = 0
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}
isPowerOfTwo(8); // true
isPowerOfTwo(15); // false4. Swapping Variables Without Temp
let a = 5; // 101 let b = 3; // 011 a = a ^ b; // a = 110 (6) b = a ^ b; // b = 101 (5) a = a ^ b; // a = 011 (3) console.log(a, b); // 3, 5 - swapped!
Performance Benefits
Use bitwise operations when the data format is defined in bits. Treat speed claims as something to measure in your own workload; a shift can change the result through 32-bit truncation in JavaScript.
| Operation | Normal Code | Bitwise Alternative | Speedup |
|---|---|---|---|
| Multiply by 2 | x * 2 | x << 1 | 10-50x |
| Divide by 2 | x / 2 | x >> 1 | 10-50x |
| Modulo power of 2 | x % 8 | x & 7 | 5-20x |
| Check even/odd | x % 2 == 0 | (x & 1) == 0 | 5-10x |
| Floor to power of 2 | Complex math | Bit hacks | 100x+ |
Common Bit Masking Patterns
Useful Bit Masks
0xFF = 1111 1111 (extract lowest byte)
0x0F = 0000 1111 (extract lower nibble)
0xF0 = 1111 0000 (extract upper nibble)
0x80 = 1000 0000 (check highest bit)
0x01 = 0000 0001 (check lowest bit)
Conclusion
Masks and flags are useful when you need to work with packed data. Check the operand width and signedness before using a bitwise operation, especially in JavaScript, where Number operands are converted to 32-bit integers.
Practice these concepts using our interactive bitwise playgroundto visualize how binary operations work in real-time.
Check the examples
Technical reference
Related Articles

Bitwise Flags in Practice: Permissions, Toggles, Masks
Use AND, OR, and XOR to pack feature flags and permission bits into a single integer, with examples you can try in the bitwise lab.

Signed Binary and Two's Complement (Negative Numbers)
How computers store negative integers: invert the bits, add one, and check the range of an 8-bit signed value.
Hexadecimal vs Binary: When to Use Each Format
Comprehensive comparison of hexadecimal and binary number systems. Learn which format to use for programming, debugging, and data representation.
