Bitwise operations are operations performed on numbers at the bit level. JavaScript provides a set of operators to perform bitwise operations. These operators are extremely useful in situations requiring low-level programming and performance optimization. In this article, we will examine bitwise operations in JavaScript and how to use them with example code snippets.
Bitwise Operators
In JavaScript, bitwise operators treat numbers as 32-bit integers and perform operations at the bit level. Here are the basic bitwise operators:
- AND (&)
- OR (|)
- XOR (^)
- NOT (~)
- Left Shift (<<)
- Sign-propagating Right Shift (>>)
- Zero-fill Right Shift (>>>)
AND (&)
The AND operator compares each bit of two operands and results in 1 if both bits are 1, otherwise 0.
let a = 5; // 0101
let b = 3; // 0011
let result = a & b; // 0001 (1)
console.log(result); // Output: 1
OR (|)
The OR operator compares each bit of two operands and results in 1 if either bit is 1.
let a = 5; // 0101
let b = 3; // 0011
let result = a | b; // 0111 (7)
console.log(result); // Output: 7
XOR (^)
The XOR operator compares each bit of two operands and results in 1 only if one of the bits is 1.
let a = 5; // 0101
let b = 3; // 0011
let result = a ^ b; // 0110 (6)
console.log(result); // Output: 6
NOT (~)
The NOT operator inverts all the bits of a single operand.
let a = 5; // 0101
let result = ~a; // 1010 (JavaScript interprets this as -6)
console.log(result); // Output: -6
Left Shift (<<)
The Left Shift operator shifts the bits of a number to the left by a specified number of positions. The bits that fall off are discarded, and the vacant positions are filled with 0s.
let a = 5; // 0101
let result = a << 1; // 1010 (10)
console.log(result); // Output: 10
Sign-propagating Right Shift (>>)
The Sign-propagating Right Shift operator shifts the bits of a number to the right by a specified number of positions. The vacant positions are filled with the sign bit of the original number (0 for positive numbers, 1 for negative numbers).
let a = -10; // 11110110 (in 8-bit representation)
let result = a >> 1; // 11111011 (-5)
console.log(result); // Output: -5
Zero-fill Right Shift (>>>)
The Zero-fill Right Shift operator shifts the bits of a number to the right by a specified number of positions. The vacant positions are always filled with 0s, regardless of the sign of the number.
let a = -10; // 11110110 (in 8-bit representation)
let result = a >>> 1; // 01111101 (in 32-bit representation, 2147483643)
console.log(result); // Output: 2147483643
Applications of Bitwise Operators
Bitwise operators are used in various fields:
Masking
Used to check or modify specific bits of a number.
let value = 0b1010; // 10 in decimal
let mask = 0b0100; // 4 in decimal
let masked = value & mask; // 0000 (0)
console.log(masked); // Output: 0
Bit Manipulation
Used for flag handling, setting or clearing specific bits.
let flags = 0b1010; // 10 in decimal
// Set the 1st bit
flags = flags | 0b0001; // 1011 (11)
console.log(flags); // Output: 11
// Clear the 3rd bit
flags = flags & ~0b1000; // 0011 (3)
console.log(flags); // Output: 3
Performance Optimization
Some mathematical operations can be done faster using bitwise operators.
// Multiply by 2
let value = 5;
let result = value << 1; // 10
console.log(result); // Output: 10
// Divide by 2
result = value >> 1; // 2
console.log(result); // Output: 2
Conclusion
Bitwise operators offer a powerful and flexible toolkit in JavaScript. These operators allow you to perform bit-level operations on numbers and can be used in various fields such as performance optimization, bit manipulation, and flag handling. Understanding and correctly using bitwise operators will help you write more efficient and effective code in JavaScript.