The **Bitwise AND** (**&**) **Operator** allows us to process a multiplication on numeric values based on their **binary** representation bit per bit.

Wikipedia actually explains it very well with examples covering the operations.

Note that the operation must be applied to equal lengths numbers.

## Area of use

We focus here on the **bitwise and operator** and its various application fields.

Usually implementing this **& operator** is intend to optimize the performance of some operations. This is particularly true for old processor or very weak ones.

World of programming standards coupled with clean code habits, the question of using it should be asked.

However there are real world use cases where these kind of operations are absolutely necessary and some process are heavy.

For instance when working on **algorithms** in these kind of fields :

- Cryptography (encryption/decryption)
- Network packets for hash comparisons and other kind of bit operations
- Mathematical data processing
- Game programming (puzzles…)
- Image processing and analysis
- …

Today we will only see a simple example of use and illustrate it in SQL and in Java.

For those who are curious about using the operator, take a look at the practical examples on GeeksForGeeks.

## Bitwise And operator examples

The most simple use of this operator is to check whether a number is even or not :

```
SELECT 6 & 1 <> 1;
```

The above SQL query will give you : true

Here is the operation computed :

```
110
AND 001
---
000
```

The resulting 0 shows that it does not match. The rest of the operation is not of *odd* type.

You can test using any decimal number of your like.

Now let’s get a bit further by **flagging** some elements in order to make the **comparison** with the **target value**.

Let’s say we have a number stored in database which identifies an Object.

This is straightforward if the ID is unique but in case we need the number to match several objects ids at the same time, how do you proceed ?

### Proposition

Take the following types of objects we could match using a single numeric value :

```
enum ObjectType {
TYPE1(0b1),
TYPE2(0b10),
TYPE3(0b100),
TYPE4(0b1000);
private long id;
ObjectType(long id) {
this.id = id;
}
}
```

The above code is illustrated using an **enum** in **Java**. We also opted to use the binary representation for numeric values as we are talking about **Bit operations** !

Now I want a numeric value matching both TYPE1 and TYPE2, this is as simple as making the following operations :

```
####
AND 0001
----
0001
```

```
####
AND 0010
----
0010
```

*What is your guess ?*

I bet you picked up : **0011** to match both types !

Checkout the Java unit test covering this example :

```
@Test
void bitwise_test() {
Assertions.assertThat(5 & ObjectType.TYPE1.id).isEqualTo(ObjectType.TYPE1.id);
Assertions.assertThat(5 & ObjectType.TYPE2.id).isNotEqualTo(ObjectType.TYPE2.id);
Assertions.assertThat(5 & ObjectType.TYPE3.id).isEqualTo(ObjectType.TYPE3.id);
Assertions.assertThat(5 & ObjectType.TYPE4.id).isNotEqualTo(ObjectType.TYPE4.id);
}
```

We could have written 5 as : 101.

All right ! I hope you got the point on using this **AND operator** and I encourage you to follow the resources disposed on this page for your own knowledge.

Beware that most of the time in your applications you will likely implement a more readable way to develop functionalities.

## Leave a Reply