Bitwise Algorithms Basics
Bitwise algorithms are used to perform operations at the bit-level or to manipulate bits in different ways. Bitwise operations are extremely fast because they are executed directly by the CPU, making them a powerful tool to optimize program efficiency.
For example, to check if a number is even or odd, we can use the Bitwise-AND (&) operator. If the last bit of the number is set, the number is odd; otherwise, it is even. Therefore, if (num & 1) !== 0 then num is odd, otherwise it is even.
Bitwise Operators
Bitwise operators work on the binary representations of numbers (which are treated as 32-bit signed integers in JavaScript during bitwise operations). Below is a summary of the six types of bitwise operators:
| Operator | Name | Description | Example |
|---|---|---|---|
& | Bitwise AND | Bit is 1 only if both bits are 1 | 5 & 3 = 1 |
| | Bitwise OR | Bit is 1 if at least one of the bits is 1 | 5 | 3 = 7 |
^ | Bitwise XOR | Bit is 1 if the bits are different | 5 ^ 3 = 6 |
<< | Left Shift | Shifts bits to the left (multiplies by 2^n) | 5 << 1 = 10 |
>> | Right Shift | Shifts bits to the right (divides by 2^n, keeps sign) | 5 >> 1 = 2 |
~ | Bitwise NOT | Inverts all bits (1 becomes 0, and 0 becomes 1) | ~5 = -6 |
In JavaScript, numbers are stored as 64-bit floating-point numbers (IEEE 754). However, bitwise operators convert their operands into 32-bit signed integers before performing the operation.
Here, we will cover the first three operators in detail.
Core Operators Deep Dive
1. Bitwise AND (&)
- Symbol:
&(Typed usingShift + 7) - Output:
1only if both bits are1, otherwise0.
Truth Table
| A | B | A & B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example
let x = 2; // Binary: 00000000000000000000000000000010
let y = 6; // Binary: 00000000000000000000000000000110
console.log(x & y); // Output: 2 (Binary: 00000000000000000000000000000010)Only the second bit is set in both numbers, hence the result is 2.
2. Bitwise OR (|)
- Symbol:
|(Typed usingShift + \) - Output:
1if at least one bit is1, otherwise0.
Truth Table
| A | B | A | B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example
let x = 2; // Binary: 00000000000000000000000000000010
let y = 6; // Binary: 00000000000000000000000000000110
console.log(x | y); // Output: 6 (Binary: 00000000000000000000000000000110)Any bit that is 1 in either number becomes 1 in the result.
3. Bitwise XOR (^)
- Symbol:
^(Typed usingShift + 6) - Output:
1only if the two bits are different, otherwise0.
Truth Table
| A | B | A ^ B |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example
let x = 2; // Binary: 00000000000000000000000000000010
let y = 6; // Binary: 00000000000000000000000000000110
console.log(x ^ y); // Output: 4 (Binary: 00000000000000000000000000000100)Bits that differ remain set in the result.
Important Facts about Bitwise Operators
- Shift Limit with Negatives: The left-shift (
<<) and right-shift (>>) operators cannot be used with negative numbers in standard bitwise contexts, as shifting negative numbers behaves differently due to the sign bit. - XOR's Interview Prominence: The bitwise XOR operator is the most useful operator from a technical interview perspective because of its unique properties (e.g.
x ^ x = 0andx ^ 0 = x). We will see some very useful applications of the XOR operator later in the course. - Do Not Substitute Logical Operators: Bitwise operators should not be used in place of logical operators (
&&,||,!) because they do not perform short-circuit evaluation and operate on numbers rather than booleans. - Powers of 2 Equivalence: The left-shift (
<<) and right-shift (>>) operators are equivalent to multiplication and division by powers of 2 respectively:x << yis equivalent tox * 2^y.x >> yis equivalent toMath.floor(x / 2^y).
- Even/Odd Verification: The
&operator can be used to quickly check if a number is odd or even. The value of expression(x & 1)is non-zero (specifically1) only ifxis odd; otherwise, the value is0.