JavaScript Bitwise Operations
12 April 2025 | Category: JavaScript
Bitwise operations are a low-level but powerful feature in JavaScript that allow you to work directly with the binary representations of numbers. These operations are commonly used in systems programming, graphics, cryptography, and situations requiring performance optimization. This tutorial provides an in-depth and plagiarism-free explanation of JavaScript bitwise operators.
1. Understanding Binary Numbers
Every number in JavaScript is stored as a 64-bit floating-point number (IEEE 754 format). However, for bitwise operations, JavaScript treats numbers as 32-bit signed integers. This means that during bitwise operations, numbers are converted to 32-bit integers, operated upon, and then converted back to 64-bit numbers.
Example:
let num = 5; // Binary: 00000000000000000000000000000101
let num2 = ~num; // Bitwise NOT operation
console.log(num2); // Output: -6
2. Types of Bitwise Operators
a. AND (&
)
Performs a bitwise AND on each pair of corresponding bits of two numbers.
5 & 3 // 0101 & 0011 => 0001 => 1
b. OR (|
)
Performs a bitwise OR operation.
5 | 3 // 0101 | 0011 => 0111 => 7
c. XOR (^
)
Performs a bitwise exclusive OR. The result is 1
only if the corresponding bits are different.
5 ^ 3 // 0101 ^ 0011 => 0110 => 6
d. NOT (~
)
Performs a bitwise NOT. Inverts each bit (0 to 1, 1 to 0).
~5 // ~00000000000000000000000000000101 => 11111111111111111111111111111010 => -6
e. Left Shift (<<
)
Shifts the bits to the left by a specified number of positions. New bits on the right are zero.
5 << 1 // 00000101 << 1 => 00001010 => 10
f. Signed Right Shift (>>
)
Shifts bits to the right, preserving the sign bit (leftmost bit).
-8 >> 2 // 11111000 >> 2 => 11111110 => -2
g. Unsigned Right Shift (>>>
)
Shifts bits to the right and fills in zeros from the left. Sign bit is not preserved.
-8 >>> 2 // 11111111111111111111111111111000 >>> 2 => 00111111111111111111111111111110 => 1073741822
3. Practical Use Cases
a. Permissions and Flags
Bitwise operators are often used to manage multiple boolean options efficiently.
const READ = 1; // 0001
const WRITE = 2; // 0010
const EXECUTE = 4;// 0100
let permissions = READ | WRITE; // 0011 => can read and write
// Check permission
console.log((permissions & READ) !== 0); // true
console.log((permissions & EXECUTE) !== 0); // false
b. Swapping Variables (XOR Swap Algorithm)
let a = 5, b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a, b); // 3 5
c. Optimizing Calculations
You can use left and right shifts to multiply or divide by powers of two:
let x = 4;
console.log(x << 1); // 8 (x * 2)
console.log(x >> 1); // 2 (x / 2)
4. Important Notes
- Bitwise operations can only be used with integers.
- Be careful with signed and unsigned shifts especially when working with negative numbers.
- JavaScript automatically converts operands to 32-bit signed integers, which may lead to unexpected results in some cases.
5. Conclusion
Bitwise operators in JavaScript offer efficient ways to manipulate binary data, manage flags, and perform quick mathematical operations. Though not commonly used in high-level web development, understanding them adds to your toolkit and opens up performance and memory optimization opportunities.
Always test thoroughly when using bitwise logic, especially with negative numbers or when mixing bitwise and regular arithmetic operations.