Hello, readers! In this article, we will be focusing on **5 NumPy Bitwise Operations** that we should know!

So, let us get started!

To begin with, Bitwise operators help us to perform bit level operations i.e. bit by bit operations through a layer of abstraction enclosed within functions.

In the course of the topic, we would be covering the below topics as a part of this article–

**AND operation****OR operation****XOR operation****Invert operation****Integer to Binary representation**

Let us begin! 🙂

## 1. NumPy Bitwise Operations – AND

The **NumPy Bitwise AND operator** enables us to perform bitwise AND operation on the array like input values. That is, it performs AND operation on the binary representation of the input integer values altogether.

**Syntax:**

```
numpy.bitwise_and(num1,num2)
```

**Example:**

In the below example, the bitwise_and() function converts the integer values 2 and 3 to their equivalent binary values i.e. 2 ~ 010 and 3 ~ 011. Further, it performs the AND operation which returns 1 as the resultant bit if both the equivalent bits are 1, else it returns 0.

```
import numpy as np
x = 2
y = 3
print ("x=",x)
print ("y=",y)
res_and = np.bitwise_and(x, y)
print ("Bitwise AND result: ", res_and)
```

**Output:**

```
x= 2
y= 3
Bitwise AND result: 2
```

## 2. Bitwise OR operation

Like AND operation, **NumPy** also provides us with `numpy.bitwise_or() function`

that enables us to perform the NumPy Bitwise Operations “OR” on the data values.

**Syntax:**

```
numpy.bitwise_or(num1,num2)
```

**Example:**

In this example, bitwise_or() function performs OR operation on the two integer values. In the OR operation, if the bits are same i.e. 0/0, else it returns zero(0), else it returns 1.

```
import numpy as np
x = 2
y = 3
print ("x=",x)
print ("y=",y)
res_or = np.bitwise_or(x, y)
print ("Bitwise OR result: ", res_or)
```

**Output:**

```
x= 2
y= 3
Bitwise OR result: 3
```

## 3. Bitwise XOR operation

The XOR operation is one of the NumPy Bitwise Operations. We can perform the operation using numpy.bitwise_xor() function. With this, we can easily perform the bitwise XOR operations on the bit-by-bit data used.

**Example:**

```
import numpy as np
x = 2
y = 3
print ("x=",x)
print ("y=",y)
res_xor = np.bitwise_xor(x, y)
print ("Bitwise XOR result: ", res_xor)
```

**Output:**

```
x= 2
y= 3
Bitwise XOR result: 1
```

## 4. Bitwise Invert operation

The bitwise invert operation is performed using numpy.invert() function. By this, we mean it performs the bit-wise NOT operation on the data bits that are internally worked up on as binary representation format.

In case of signed integers, two’s complement value is returned.

**Example:**

```
import numpy as np
x = 2
y = 3
print ("x=",x)
res = np.invert(x)
print ("Bitwise Invert operation result: ", res)
```

**Output:**

```
x= 2
Bitwise Invert operation result: -3
```

## 5. Binary representation

It is possible for us to explicitly convert the integer values to binary ones, using NumPy module. The` binary_repr() `

function enables us to convert an integer data value to binary value easily.

**Syntax:**

```
numpy.binary_repr()
```

**Example:**

```
import numpy as np
x = 7
print ("x=",x)
res = np.binary_repr(x)
print ("Bitwise representation of x: ", res)
```

**Output:**

In this example, we have converted the int value ‘7’ to its equivalent binary representation.

```
x= 7
Bitwise representation of x: 111
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, stay tuned with us.

Till then, Happy Learning!! 🙂