Bit manipulation involves applying logical operations on a sequence of bits. Bit manipulation is an increasingly common topic in coding interviews, and they expect you to understand how to use bitwise operations.
When it comes to bitwise manipulation, AND (&
) is one of the most commonly used logical bitwise operators. AND compares two operands of equal length.
In this tutorial, we will take a deep dive into the AND operator with some useful code examples you could expect to see in a coding interview.
This guide at a glance:
In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills.
Master Solving Problems using Bit Manipulation
In computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the bit level. Bitwise operations take bit patterns and manipulate or compare them according to the operator used. Bit manipulation uses a constant time complexity.
Bitwise operators are directly supported by the processor, so they are simpler and faster than arithmetic operations. Bit manipulation is used in low-level device control, error detection and correction algorithms, data compression, and encryption algorithms.
There are several types of bitwise operators we use, the most common being:
Bitwise AND Operator &
: Sets each bit to 1 if both bits are 1
Bitwise OR Operator |
: Sets each bit to 1 if one of two bits is 1
One’s complement / NOT Operator ~
: Inverts all the bits
Bitwise XOR Operator ^
: Sets each bit to 1 if only one of two bits is 1
Bitwise Left shift Operator<<
: Shifts bits to the left
Bitwise Right shift Operator >>
: Shifts bits to the right
Note: JavaScript stores numbers as 64 bits floating point numbers; however, bitwise operations are performed on 32 bits binary numbers.
This means that before it perfroms a bitwise operation, JavaScript will convert any numbers to 32 bits signed integers and it is then converted back to 64 bits after operation.
Now let’s dive into the AND operator. Bitwise AND (&
) takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits.
The AND operator will return a 1
for each bit position where the corresponding bits of both operands are also 1
. So, if two input bits are 1
, the output is 1
. In all other cases, it returns 0
.
The basic syntax looks like this:
a & b
Take a look at this code example:
const a = 5; // 00000000000000000000000000000101const b = 3; // 00000000000000000000000000000011console.log(a & b); // 00000000000000000000000000000001
Essentially, the AND operator is doing the following:
&
operator is applied to each pair of bits. If both bits are 1
, the corresponding result bit is set to 1
. Otherwise, the corresponding result bit is set to 0
.a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Learn bit manipulation without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments - making learning quick and efficient.
// bitwise AND operator examplelet a = 12;let b = 25;result = a & b;console.log(result);
Here, the binary value of 12 is 00000000000000000000000000001100
, and the binary value of 25 is 00000000000000000000000000011001
. So, when bitwise &
operation is performed, the binary result will be 00000000000000000000000000001000
, which converts back into the decimal value 8
.
Let’s take a more complicated example of the &
operator. Here, we have a program that will for check even or odd numbers using the &
operator.
Input = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output: { "Odd" , "Even" , "Odd" , "Even" , "Odd" , "Even" , "Odd" , "Even" , "Odd" }
const IsEven = n => {return (n & 1) === 0 ? 'Even' : 'Odd';}const firstNumber = 125;const secondNumber = 8;console.log (`Number '${firstNumber}' is : ${IsEven (firstNumber)}`);console.log (`Number '${secondNumber}' is : ${IsEven (secondNumber)}`);
The &
operator can be used for bit masking applications to ensure that certain bits are “turned off”. For example, imagine we have an 8-bit integer, and we want to make sure that the first 4 bits are set to 0 (or turned off).
We can do this by creating a bit mask, where the first 4 bits are set to 0
, and all other bits are set to 1
. They we perform an &
bitwise operation.
const mask = 0b11110000;// 222 => 11011110// (222 & mask)// ------------// 11011110// & 11110000// ------------// = 11010000// ------------// = 208 (decimal)console.log(222 & mask);
You should now have a good idea what the AND bitwise operator is and how it can be used in your programs. There is still more to learn. Next, you should check out the other bitwise operators in detail with hands-on practice like:
To get started with some hands-on bitwise problems, check out Educative’s course Master Solving Problems using Bit Manipulation. In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills.
By the end, you will be able to solve problems faster with greater efficiency and understand any Bitwise question that comes your way.
Happy learning!
Join a community of more than 1.4 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.