> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Bitwise AND, Computations, and Examples
- URL: https://www.javahandbook.com/bitmask/bitwise-and-computations-and-examples/
- Published: 2024-06-16T06:26:03.000Z
- Updated: 2025-08-07T03:13:46.000Z
- Author: Gopi Gorantala
- Tags: AND Operator, #docs, #bitmask

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

```json
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](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-1---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands.svg)

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](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-2---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands-1.svg)

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](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-3---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands-3.svg)

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](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-4---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands.svg)

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](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-5---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands.svg)

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

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

### Code

### Java

```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

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

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

### JavaScript

```js
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++

```cpp
#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

```ts
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.

![](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-3---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands-1.svg)

![](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2025/08/Figure-3---Bitwise-AND-gate-8-bit-representation-and-computation-of-two-operands-2.svg)