In JavaScript, >>> is the zero-fill right shift operator. It shifts the first operand’s binary representation to the right by the number of places specified in the second operand, and it fills the left with zeros. It’s often used for unsigned bit manipulation.
Bitwise AND (&) in JavaScript: A 5-minute guide
Key takeaways:
Bit manipulation applies logical operations on binary values, offering efficient computation with
complexity. Common bitwise operators include AND (
&), OR (|), NOT (~), XOR (^), and bit-shifting (<<,>>).Bit manipulation is widely used in device control, encryption, data compression, and error correction.
The bitwise AND operator performs a logical AND on binary numbers, commonly used for tasks like checking even/odd and bit masking.
JavaScript performs bitwise operations on 32-bit integers, converting them back to 64-bit after execution.
In programming, both speed and efficiency matter. While most developers rely on arithmetic operations for number manipulation, bitwise operations provide an even faster alternative. They allow direct manipulation of binary data at the bit level, making them faster, memory-efficient, and widely used in low-level programming, cryptography, and optimization problems.
Grokking Dynamic Programming Interview in JavaScript
Some of the toughest questions in technical interviews require dynamic programming solutions. Dynamic programming (DP) is an advanced optimization technique applied to recursive solutions. However, DP is not a one-size-fits-all technique, and it requires practice to develop the ability to identify the underlying DP patterns. With a strategic approach, coding interview prep for DP problems shouldn’t take more than a few weeks. This course starts with an introduction to DP and thoroughly discusses five DP patterns. You’ll learn to apply each pattern to several related problems, with a visual representation of the working of the pattern, and learn to appreciate the advantages of DP solutions over naive solutions. After completing this course, you will have the skills you need to unlock even the most challenging questions, grok the coding interview, and level up your career with confidence. This course is also available in C++, Java, and Python—with more coming soon!
One of the most fundamental bitwise operators is the bitwise AND (&) operator. This operator is commonly used to check even/odd numbers, set or clear specific bits, perform bit masking, and handle low-level optimizations.
Understanding bitwise operations is a must-have skill if you’re preparing for coding interviews or looking to optimize your JavaScript code. By the end of this blog, you’ll have a clear grasp of how the AND (&) operator works, its practical applications, and how to use it efficiently in JavaScript.
What are bitwise operators?#
Before diving into the bitwise AND (&) operator, let’s quickly understand bitwise operations and how they work in JavaScript.
A bitwise operation manipulates individual bits of a binary number rather than performing operations on whole numbers. Since computers store everything in binary (0s and 1s), bitwise operations allow you to interact directly with binary representations instead of using high-level arithmetic functions.
If you’re eager to dive deeper into bit-level operations and sharpen your problem-solving skills for coding interviews, check out the following Educative’s comprehensive course. It covers everything from fundamental bitwise operations to advanced problem-solving techniques, equipping you with the knowledge needed to tackle even the trickiest bit manipulation challenges in FAANG and competitive programming interviews.
Master how bit-level operations are computed
This course teaches bit manipulation, a powerful technique to enhance algorithmic and problem-solving skills. It is a critical topic for those preparing for coding interviews for top tech companies, startups and industry leaders. Competitive programmers can take full advantage of this course by running most of the bit-related problems in O(1) complexity. The course will begin by educating you about the number system and its representation, decimal and binary, followed by the six bitwise operators: AND, OR, NOT, XOR, and bit-shifting (left, right). You will receive ample practical experience working through practice problems to improve your comprehension. Upon completing this course, you will be able to solve problems with greater efficiency and speed.
Why use bitwise operations?#
Super fast: Directly supported by the processor, making them faster than arithmetic operations.
Constant time complexity
: No matter how large the numbers are, bitwise operations take the same amount of time. Low-level programming: Used in encryption, compression, graphics, networking, and security protocols.
Memory efficient: Helps optimize data storage and perform compact calculations.
Common bitwise operators in JavaScript#
Operator | Symbol | Function |
Bitwise AND |
| Sets each bit to 1 if both corresponding bits are 1. |
Bitwise OR |
| Bitwise OR ( |
Bitwise XOR |
| Sets each bit to 1 if only one of the corresponding bits is 1. |
Bitwise NOT |
| Inverts all bits (1s become 0s, and vice versa). |
Left Shift |
| Shifts bits to the left (multiplies by 2). |
Right Shift |
| Shifts bits to the right (divides by 2). |
How bitwise operations work under the hood#
JavaScript stores numbers as 64-bit floating-point values (IEEE 754 standard), but bitwise operations are performed on 32-bit signed integers. What does this mean? It means:
Before executing a bitwise operation, JavaScript converts numbers to 32-bit signed integers.
After performing the operation, the result is converted back to a 64-bit floating-point number.
Understanding the bitwise AND (&) operator#
The bitwise AND (&) operator performs a logical AND operation on each corresponding bit of two numbers. Note:
If both corresponding bits are
1, the result is1.Otherwise, the result is
0.
It can be visualized as a series circuit like this:
When both switches are on, the light is on. If any of the switches is off, the bulb does not light, resulting in the output being 0.
Example: Performing AND (&) on two numbers#
Let’s break it down using 12 & 25 in JavaScript.
Let’s understand how the result is calculated below:
Bit Position | 12 ( | 25 ( | Result ( |
Bit 1 |
|
|
|
Bit 2 |
|
|
|
Bit 3 |
|
|
|
Bit 4 |
|
|
|
Bit 5 |
|
|
|
Bit 6 |
|
|
|
Bit 7 |
|
|
|
Bit 8 |
|
|
|
Since only one bit (position 5) is 1 in both numbers, the result is 8 (00001000).
Try this: What do you think 7 & 3 will output? Run it in your JavaScript console!
Real-world applications of the AND (&) operator#
Let’s have a look at the real-world applications:
Checking if a number is even or odd (without %)#
Did you know you can check even or odd numbers without using modulo (%)?
Why does this work?
Even numbers end in
0in binary.Odd numbers end in
1in binary.The last bit of a binary number determines if it’s odd (
1) or even (0). Usingnum & 1lets you check this efficiently.
Turning off specific bits (Bit masking)#
What if you want to turn off specific bits while keeping others unchanged?
This technique is widely used in cryptography, data compression, and security applications.
Checking if a specific bit is set#
Need to check if a specific bit in a number is 1?
How does this work?
1 << positioncreates a number where only that bit is1.Using
&checks if that bit is set (1) or not (0).
Used in permission handling, flag checking, and low-level programming.
Advanced bitwise techniques to explore#
Now that you’ve gained a solid understanding of the bitwise AND (&) operator, it’s time to build upon this knowledge and explore other essential bitwise techniques that can optimize your code, enhance your problem-solving skills, and prepare you for coding interviews.
Let’s look at the advanced bitwise techniques.
Finding a missing number in an array using XOR (^)#
A classic problem in coding interviews is finding the missing number in an array of size n containing numbers from 0 to n, but with one number missing.
Instead of using sorting ^) operator.
Why does XOR work here?
XOR of a number with itself is always
0(a ^ a = 0).XOR of a number with
0remains unchanged (a ^ 0 = a).Since numbers appear twice (except the missing one), their XOR cancels out, leaving only the missing number.
Try it out: What if there were multiple missing numbers? How would you modify this approach?
Finding the first set bit using the left shift (<<)#
The first set bit is the position of the least significant 1 bit in a number’s binary representation. This is useful in low-level bit manipulations like bitwise compression, flagging mechanisms, and optimizing memory usage.
Why does this work?
We right shift (
>>) until we find a1.The position count keeps increasing until we locate the first set bit.
Where is this used? In bitwise compression, error detection, and CPU instruction optimization.
Checking if a number is a power of 2 using AND (&)#
A number is a power of 2 if it has exactly one 1 in its binary representation (e.g., 2 (10), 4 (100), 8 (1000)).
A simple check using AND (&) can determine this:
Why does this work?
A power of
2has only one set bit (1000,10000etc.).Subtracting
1flips all bits after the rightmost1(1000 - 1 = 0111).num & (num - 1)should be0only for powers of2.
This trick is commonly used in game development, networking (buffer sizes), and cryptography!
Bitwise tricks and optimization strategies#
Now that you’ve explored bitwise AND (&) and related techniques, let’s discuss where bitwise operations excel in real-world applications:
Swapping two numbers without a temporary variable#
Instead of using extra space, XOR swapping allows you to swap two numbers without using a third variable.
Why does this work?
a ^ bstores the XOR result ina, which helps extract the originalafromband vice versa.No extra memory is required!
Counting the number of 1s in a binary representation (Hamming weight)#
Used in compression algorithms, cryptography, and AI, this technique quickly finds how many bits are set in a number.
Why does this work?
num & (num - 1)removes the rightmost 1 in each iteration, reducing unnecessary loops.This runs in
complexity, where is the number of set bits (faster than ).
Wrap up and next steps#
Mastering bitwise operations isn’t just about coding tricks—it’s about writing faster, more efficient programs. Whether you’re optimizing algorithms, working on performance-critical systems, or prepping for coding interviews, understanding how to manipulate bits is a powerful tool in your JavaScript toolkit. Next, you should check out the other bitwise operators in detail with hands-on practice, like:
- Missing number with XOR
- Get the first set bit with LEFT
- Power of 2 with AND
For more hands-on learning, check out these Educative’s blogs:
- A quick guide to bitwise operators in Java
- Computer Number Systems 101: Binary & Hexadecimal Conversions
These resources will further strengthen your understanding of bitwise operations and computer number systems.
To expand your JavaScript expertise, explore these related blogs:
If you want to deepen your knowledge, start your journey today with Educative’s JavaScript courses and transform your potential into expertise. Let’s build something incredible together.
Some of the latest courses available on our platform include:
Happy learning!
Frequently Asked Questions
What is >>> in JavaScript?
What is >>> in JavaScript?
What is bitwise in JavaScript?
What is bitwise in JavaScript?
What is the &= operator in JavaScript?
What is the &= operator in JavaScript?
What is a bitwise operation in the genetic algorithm?
What is a bitwise operation in the genetic algorithm?
What do you understand by bitwise AND operator?
What do you understand by bitwise AND operator?