DEV Community

Cover image for Understand Bitwise AND (&) with JavaScript: 5-minute tutorial
Hunter Johnson for Educative

Posted on • Originally published at

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

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:

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.

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 performs a bitwise operation, JavaScript will convert any numbers to 32 bits signed integers, and it is then converted back to 64 bits after an operation.

Understanding the AND operator

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
Enter fullscreen mode Exit fullscreen mode

Take a look at this code example:

const a = 5;        // 00000000000000000000000000000101
const b = 3;        // 00000000000000000000000000000011

console.log(a & b); // 00000000000000000000000000000001
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

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 number is longer than 32 bits, its most significant bits are discarded.
  • In the 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.


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; 
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

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" }
Enter fullscreen mode Exit fullscreen mode
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)}`);
Enter fullscreen mode Exit fullscreen mode
Number '125' is : Odd
Number '8' is : Even
Enter fullscreen mode Exit fullscreen mode

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. Then we perform an & bitwise operation.

const mask = 0b11110000;

// 222 => 11011110

// (222 & mask)
// ------------
// 11011110
// & 11110000
// ------------
// = 11010000
// ------------
// = 208 (decimal)

console.log(222 & mask); 
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

What to learn next

You should now have a good idea of 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 the first set bit with LEFT
  • Power of 2 with AND

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 on Educative

Start a discussion

What is your favorite JavaScript operator to work with? Was this article helpful? Let us know in the comments below!

Top comments (0)