6 Python Bitwise Operators [To Easily Manipulate Bits]

What is a bitwise operators? We will say that operator is a bitwise operator if its result is based on the bits representation of its integer operands. In other words, python bitwise operators performs its operation on its integer operands at a bit level.

The python programing language has 6 bitwise operators :NOT Operator : ~A , AND Operator : A & B , OR Operator : A | B , XOR Operator : A ^ B , SHIFT LEFT Operator : A << B , SHIFT RIGHT Operator : A >> B

Let’s what of those Python bitwise operators do

NOT Operator : ~A

The simplest python bitwise operator is the NOT operator, denoted by the symbol ~.

The NOT operator is the unary operator – In other words, the NOT operator takes only one integer value for its operation.

What ~A do?

The n’th bit of the result of NOT operator is 0 if corresponding bit of A is 1, and 1 if the corresponding bit of A is 0. In other words, the result is the complement of A – the number you get by switching each 1 for a 0 and each 0 for a 1. We can write this information in the following truth table :

Samples of NOT Operator

```     A  = 00110101
~A = 11001010   ```
```    A  = 00000001
~A = 11111110```
```     A  = 00110101
~A = 11001010   ```
```      A  = 01100110
~A = 10011001  ```

AND Operator : A & B

The AND operator is denoted by the symbol &.

The AND operator is the binary operator – In other words, the AND operator takes 2 integer values for its operation.

What A & B do?

The n’th bit of the result of AND operator is 1 if the corresponding bit of A AND of B is 1, otherwise it’s 0. We can write this information in the following truth table :

Samples of AND Operator

```    A     = 10001001
B     = 01011011
A & B = 00001001  ```
```    A     = 11000011
B     = 01011010
A & B = 01000010 ```
```    A     = 11110000
B     = 00111100
A & B = 00110000 ```
```    A     = 11001100
B     = 00001111
A & B = 00001100  ```

OR Operator : A | B

The OR operator is denoted by the symbol |.

The OR operator is the binary operator – In other words, the OR operator takes 2 integer values for its operation.

What the A | B do?

The n’th bit of the result of OR operator is 1 if the corresponding bit of A OR of B is 1, otherwise it’s 0. We can write this information in the following truth table :

As we can see in the truth table, There is another way to describe the OR operator The n’th bit of the result of OR operator is 0 only if the corresponding bit of A AND of B is 0, otherwise it’s 1.

Samples of OR Operator

`    A     = 11000011    B     = 01011010    A | B = 11011011`
```    A     = 11001100
B     = 00001111
A | B = 11001111```
`    A     = 11110000    B     = 00111100    A | B = 11111100`
```    A     = 10001001
B     = 01011011
A | B = 11011011```

XOR Operator : A ^ B

The XOR operator is denoted by the symbol ^.

The XOR operator is the binary operator – In other words, the XOR operator takes 2 integer values for its operation.

What the A ^ B do?

The n’th bit of the result of XOR operator is 1 if the corresponding bit of A and B are different, and 0 if they are identical (if both are 0 or both are 1). We can write this information in the following truth table :

Samples of XOR Operator

```    A     = 10001001
B     = 01011011
A ^ B = 11010010 ```
```    A     = 11000011
B     = 01011010
A ^ B = 10011001    ```
```    A     = 11001100
B     = 00001111
A ^ B = 11000011    ```
```    A     = 11110000
B     = 00111100
A ^ B = 11001100  ```

SHIFT LEFT Operator : A << B

The SHIFT LEFT operator is denoted by the symbol <<

The SHIFT LEFT operator is the binary operator – In other words, the SHIFT LEFT operator takes 2 integer values for its operation.

What the A << B do?

The result of this expression is A with the bits shifted to the left by B places. The new bits inserted in the right side are zero.

Samples of SHIFT LEFT Operator

```    00000001 << 0 = 00000001
00000001 << 1 = 00000010
00000001 << 2 = 00000100
00000001 << 5 = 00100000    ```
```    10001001 << 0 = 10001001
10001001 << 1 = 00010010
10001001 << 2 = 00100100
10001001 << 5 = 00100000```

SHIFT RIGHT Operator : A >> B

The SHIFT RIGHT operator is denoted by the symbol >>

The SHIFT RIGHT operator is the binary operator – In other words, the SHIFT RIGHT operator takes 2 integer values for its operation.

What the A >> B do?

The result of this expression is A with the bits shifted to the right by B places. The new bits inserted in the left side are the left most bit of A

Samples of SHIFT LEFT Operator

```    10000000 << 0 = 10000000
10000000 << 1 = 11000000
10000000 << 2 = 11100000
10000000 << 5 = 11111100    ```
```    01000000 << 0 = 01000000
01000000 << 1 = 00100000
01000000 << 2 = 00010000
01000000 << 5 = 00000010    ```

Python Bitwise Operators

We see what the python bitwise operators do. In the next part, we see some insights into how to manipulate bits.