Key Takeaways
- Bitwise operators manipulate binary representations of numbers for potentially faster computations.
- Bitwise operations are useful for specific tasks like checking even/odd numbers and swapping variables.
- Understanding all bitwise operators can make you a more versatile developer, even in 2026.
What are bitwise operators?
Bitwise operators work directly on the binary representations of numbers. By operating at the bit level, these operations can be more efficient than higher-level arithmetic operations. They're powerful tools for tasks that require precision and speed.
Bitwise operators with examples
Although bitwise operators operate at the bit level, they still return numeric values. Let's explore each bitwise operator with examples:
Bitwise And &
1 & 1 // returns 1
2 & 1 // returns 0
3 & 2 // returns 2
With the & operator, each bit position returns a 1 only if both operands have a 1 in that position. Consider 3 & 2:
11 // the binary representation of 3
10 // the binary representation of 2
10
Both numbers share a 1 in one bit position, making the result 2 (binary 10).
In the real world...
The & operator is often used to determine if a number is even or odd:
(x & 1) === 0
The expression evaluates to true for even numbers as their binary form ends in 0.
Bitwise Or |
1 | 1 // returns 1
2 | 1 // returns 3
3 | 2 // returns 3
The | operator sets a bit to 1 if either operand has a 1 in that bit position. For example, 3 | 2:
11 // the binary representation of 3
10 // the binary representation of 2
11
Any bit position that's 1 in either number results in a 1. Thus, 3 | 2 returns 3.
In the real world...
Use the | operator to effectively floor a decimal number:
5.23 | 0 // returns 5
Bitwise XOr ^
1 ^ 1 // returns 0
2 ^ 1 // returns 3
3 ^ 2 // returns 1
The ^ operator returns a 1 only if one of the values at a bit position is 1, but not both. Example: 3 ^ 2:
11 // the binary representation of 3
10 // the binary representation of 2
01
Since only one of the positions differ, the result is 1 (binary 01).
In the real world...
Swap variables without a temporary variable using ^:
a ^= b; b ^= a; a ^= b;
This operation toggles the values of a and b.
Bitwise Not ~
~1 // returns -2
~2 // returns -3
~3 // returns -4
The ~ operator inverts bits, turning all 1s to 0s and vice versa. Note that JavaScript uses 32-bit signed integers, where the leading bit indicates the sign.
~2
00000000000000000000000000000010 // the 32-bit representation of 2
11111111111111111111111111111101 // inverted bits, resulting in -3
In the real world...
Check if an array includes a value with a simplified technique:
!!~arr.indexOf(x)
This shorthand effectively checks for the presence of x in arr.
Bitwise Shift Operators
Zero fill left shift <<
The << operator shifts bits left, filling 0s on the right and dropping bits on the left.
Signed right shift >>
The >> operator right shifts bits, maintaining the sign bit while dropping bits from the right.
Zero fill right shift >>>
The >>> shift fills bits with 0s from the left and ignores the sign.
In the real world...
Shift operators can replace some arithmetic operations. For instance:
x << 2 // equivalent to multiplying x by 4
Conclusion
Bitwise operators continue to be relevant for performance-critical tasks, even as modern frameworks abstract much of their direct use. Understanding these operators enriches your skill set in optimizing code when necessary.
FAQ
Are bitwise operators still relevant in 2026?
Yes, while newer frameworks abstract many tasks, bitwise operators are vital for performance optimization and specific coding scenarios.
Can bitwise operators be used in other programming languages?
Absolutely! Most programming languages support bitwise operations, including Python, C++, and Java, among others.
Is using bitwise operators more performant than regular arithmetic operations?
In some cases, yes. Bitwise operations are typically faster because they directly manipulate binary data at the hardware level.
