Home/Blog/Programming/Understand Bitwise AND (&) with JavaScript: 5 minute tutorial
Home/Blog/Programming/Understand Bitwise AND (&) with JavaScript: 5 minute tutorial

Understand Bitwise AND (&) with JavaScript: 5 minute tutorial

5 min read
Jan 11, 2024

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

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:

Master how bit-level operations are computed

Cover
Grokking Bit Manipulation for Coding Interviews

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.

3hrs
Intermediate
7 Challenges
11 Quizzes

Bitwise operators refresher#

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.

For example, a common operator is the xor operator, which is represented by the caret symbol (^) in JavaScript (JS). It enables programmers to compare two binary values at the bit level and return a response that shows the bits if and only if one of the operands has a 1.

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 (as earlier mentioned)

  • 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.


Understanding the Bitwise AND(&) operator#

Now let’s dive into the AND operator.

Bitwise AND – denoted by the ampersand sign (&) is a key binary operation in computing, digital electronics, and programming. The significance of Bitwise AND stems from its wide-ranging applications and its connection to computing systems’ hardware. It performs a logical AND operation on each corresponding bit pair from two binary numbers of equal length, resulting in the creation of a new binary number.

Several programming languages like C, C++, and Python have built-in bitwise AND operators for integers, making them indispensable tools for programmers by allowing efficient binary data handling and code optimization. Its wide applicability in data manipulation, low-level programming, and bitwise arithmetic makes it a must-have component of modern computing.

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; // 00000000000000000000000000000101
const b = 3; // 00000000000000000000000000000011
console.log(a & b); // 00000000000000000000000000000001

Essentially, the AND operator is doing the following:

  • It takes two numbers.
  • The operands are converted to 32-bit integers and expressed as zeroes and ones. If a numbers is longer than 32 bits, their most significant bits are discarded.
  • In first operand, each bit is paired with the corresponding bit of the second operand.
  • The & 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

Master how bit-level operations are computed

Cover
Grokking Bit Manipulation for Coding Interviews

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.

3hrs
Intermediate
7 Challenges
11 Quizzes

Bitwise AND examples in JavaScript#

Now that we understand the basics of AND, let’s look at a few more code examples to see what the AND operator can do for us in JavaScript. First, with a simple example:

// bitwise AND operator example
let 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.


Checking even or odd#

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)}`);

Turning off bits#

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);

What to learn next#

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:

  • Missing number with XOR
  • Get first set bit with LEFT
  • Power of 2 with AND
  • and more

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!


Continue reading about Bitwise and number systems#

Frequently Asked Questions

What is >>> in JavaScript?

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.


Written By:
Amanda Fawcett
 
Join 2.5 million developers at
Explore the catalog

Free Resources