Hello, readers! In this article, we will be focusing on **NumPy Bitwise operators** in detail. So, let us begin!! ðŸ™‚

## Use of NumPy Bitwise Operators

NumPy module offers us with various operations and functions to deal with and manipulate the data altogether.

With this in place, we also know that NumPy serves as the mathematical computation model to most of the data science and machine learning problems and is the base for many data analytics libraries.

Along with this, the usual bitwise operations such as AND, OR, XOR, etc are now inculcated and replicated into the NumPy bitwise operations i.e. all the operations would be performed bit by bit.

In the context of this topic, we will be focusing on the below bitwise operators in NumPy–

**AND operator****OR operator****Invert operation****XOR operator**

Let us have a look at each one of them in the upcoming section.

### 1.Bitwise AND operator in NumPy

Like any usual Binary AND operation, NumPy offers us with the concept of Bitwise AND operation which sets a bit to 1 only when all the equivalent bits are 1, else it sets the result to 0.

In this case, to achieve Bitwise AND functionality, we make use of bitwise_and() function.

**Syntax–**

```
numpy_bitwise_and()
```

**Example–**

```
import numpy as np
a = 3
b = 2
data_and = np.bitwise_and(a, b)
print ("NumPy Bitwise AND result: ", data_and)
```

**Output–**

```
NumPy Bitwise AND result: 2
```

### 2. XOR operator

The standard XOR operation can be inculcated even in the NumPy module with the help of bitwise_xor() function. With this function, we can perform the XOR operation in a bit by bit manner.

**Syntax–**

```
numpy.bitwise_xor(x, y)
```

**Example–**

As seen in the below example, we have perform 3 XOR operation on 3, 2. First, internally, it converts the numbers into their binary format. 3 ~ 011 and 2 would be 010. Applying the logic of XOR operation, the resultant value will be 001 because it would result into 1 bit if the data is 0/1 or 1/0 or 0/0 and the result bit would be 0 if the data is 1/1.

```
import numpy as np
a = 3
b = 2
xor = np.bitwise_xor(a, b)
print ("NumPy Bitwise XOR result: ", xor)
```

**Output–**

```
NumPy Bitwise XOR result: 1
```

### 3. OR operator

With NumPy module, we can perform Bitwise OR operation by making use of the below function-

**bitwise_or() function**

In OR operation, the resultant bits are equal to 0 only when all the equivalent bits are the same i.e. either 0/0 or 1/1. Else, any other combinations results into 1 as the outcome.

**Syntax–**

```
numpy.bitwise_or()
```

**Example–**

```
import numpy as np
a = 3
b = 2
data_or = np.bitwise_or(a, b)
print ("NumPy Bitwise OR result: ", data_or)
```

**Output–**

```
NumPy Bitwise OR result: 3
```

### 4. Invert Operation

Even the Invert functionality seems to be pretty one liner in NumPy module.

Yes, with invert() function, we can easily perform the inversion of the data/number in a bit by bit manner easily. Internally, it converts the number into its binary form and then perform inversion on the same making use of two’s complement.

**Syntax–**

```
numpy.invert()
```

**Example–**

```
import numpy as np
a = 3
b = 2
inv = np.invert(a)
print ("NumPy Bitwise Invert result: ", inv)
```

**Output–**

```
NumPy Bitwise Invert result: -4
```

## Conclusion

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

Try implementing the concept of NumPy bitwise operations and let us know your experience about the same in the comment box.

For more such posts related to Python Programming, Stay tuned with us!

Till then, Happy Learning!! ðŸ™‚