Skip to main content

AND Operator

Bitwise AND, Computations, and Examples

3 min read

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.

Step#01:

Figure 1 - Bitwise AND gate 8-bit representation and computation of two operands
Figure 1 - Bitwise AND gate 8-bit representation and computation of two operands

Step#02:

Figure 2 - Bitwise AND gate 8-bit representation and computation of two operands
Figure 2 - Bitwise AND gate 8-bit representation and computation of two operands

Step#03:

Figure 3 - Bitwise AND gate 8-bit representation and computation of two operands
Figure 3 - Bitwise AND gate 8-bit representation and computation of two operands

Step#04:

Figure 4 - Bitwise AND gate 8-bit representation and computation of two operands
Figure 4 - Bitwise AND gate 8-bit representation and computation of two operands

Step#05:

Figure 5 - Bitwise AND gate 8-bit representation and computation of two operands
Figure 5 - Bitwise AND gate 8-bit representation and computation of two operands

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.

Reading Progress