# Bitwise AND, Computations, and Examples

A Bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits. This is equivalent to multiplying them.

Here, we discuss the '&' operator in more detail.

## Introduction

A Bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits. This is equivalent to multiplying them.

Bitwise ANDing any number x with 0 yields 0

## Bitwise AND

The Bitwise AND operator does the following:

• It is a binary operator that takes two numbers.
• When we do Bitwise & of these two numbers, it considers the binary representation of these two numbers.
• Bitwise AND compares bit by bit in binary representation, and whatever binary representation you get, the corresponding integer is returned as an output.

The integer data type takes `4` bytes in C, C++, and Java programming languages.

``4 bytes = 4 * (8 bits) = 32 bits. {1 byte = 8 bits}``

## Example

Let’s see one of the above examples in binary representation.

For simplicity, let’s represent these in 16-bit instead of 32-bit binary.

``````a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a & b       : 0000 0000 0000 1000
---------------------------------``````

The `&` operation starts from the rightmost bit traversing towards the leftmost bit in the 32-bit binary representation.

## Illustration

Let’s visualize the steps below.

Below is a code representation of the `&` operator.

## Code

### Java

``````class AndOperation {
public static void main( String args[] ) {
int x = 12;
int y = 10;
System.out.println("Bitwise AND of (" + x + " , " + y + ") is: " + (x & y)); // yields to 8
}
}

// Outputs: Bitwise AND of (12 , 10) is: 8``````

### Python

``````x = 12
y = 10
print("Bitwise AND of (x ,y) is : ", (x & y))

# Output: Bitwise AND of (x ,y) is: 8``````

### JavaScript

``````const AndOperation = (x, y) => {
return x & y;
}

const x = 12;
const y = 10;

console.log (`Bitwise AND of (\${x}, \${y}) is: \${AndOperation (x, y)}`);

// Output: Bitwise AND of (12, 10) is: 8``````

### C++

``````#include <iostream>
using namespace std;

int main() {
int x = 12;
int y = 10;
cout << "Bitwise AND of (x ,y) is: " << (x & y); // yields to 8
return 0;
}

// Output: Bitwise AND of (x ,y) is: 8``````

### TypeScript

``````export const AndOperation = (x: number, y: number): number => {
return x & y;
}

const x: number = 12;
const y: number = 10;

console.log(`Bitwise AND of (\${x}, \${y}) is: \${AndOperation(x, y)}`);

// Output: Bitwise AND of (12, 10) is: 8``````

We will apply the Bitwise approach to solve a few problems in the following lessons.

Let’s jump into our first problem with the Bitwise `&` operator.

Bit ManipulationData Structures and Algorithms

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Members Public

## Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Members Public

## Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Members Public

## Bit Manipulation Course Overview

Overview 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. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things