What is a bitwise operator? 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 operator 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

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 the ~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 :

A | ~A |
---|---|

0 | 1 |

1 | 0 |

```
A =
```**0000000**1
~A = **1111111**0

```
A =
```**0**11**00**11**0**
~A = **1**00**11**00**1**

```
A =
```**00**11**0**1**0**1
~A = **11**00**1**0**1**0

```
A =
```**00**11**0**1**0**1
~A = **11**00**1**0**1**0

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 the 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 :

A | B | A & B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

```
A = 1000
```**1**00**1**
B = 0101**1**01**1**
A & B = 0000**1**00**1**

```
A = 1
```**1**0000**1**1
B = 0**1**0110**1**0
A & B = 0**1**0000**1**0

```
A = 11
```**11**0000
B = 00**11**1100
A & B = 00**11**0000

```
A = 1100
```**11**00
B = 0000**11**11
A & B = 0000**11**00

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 :

A | B | A | B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

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.

```
A = 10
```**0**01**0**01
B = 01**0**11**0**11
A | B = 11**0**11**0**11

```
A = 11
```**0**00**0**11
B = 01**0**11**0**10
A | B = 11**0**11**0**11

```
A = 111100
```**00**
B = 001111**00**
A | B = 111111**00**

```
A = 11
```**00**1100
B = 00**00**1111
A | B = 11**00**1111

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 :

A | B | A ^ B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

```
A =
```**10**0**0**10**0**1
B = **01**0**1**10**1**1
A ^ B = **11**0**1**00**1**0

```
A =
```**1**10**00**01**1**
B = **0**10**11**01**0**
A ^ B = **1**00**11**00**1**

```
A =
```**11**11**00**00
B = **00**11**11**00
A ^ B = **11**00**11**00

```
A =
```**11**0011**00**
B = **00**0011**11**
A ^ B = **11**0000**11**

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.

```
```**00000001** << 0 = **00000001**
**00000001** << 1 = **0000001**0
**00000001** << 2 = **000001**00
**00000001** << 5 = **001**00000

```
```**10001001** << 0 = **10001001**
**10001001** << 1 = **0001001**0
**10001001** << 2 = **001001**00
**10001001** << 5 = **001**00000

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

```
```**10000000** << 0 = **10000000**
**10000000** << 1 = 1**1000000**
**10000000** << 2 = 11**100000**
**10000000** << 5 = 11111**100**

```
```**01000000** << 0 = **01000000**
**01000000** << 1 = 0**0100000**
**01000000** << 2 = 00**010000**
**01000000** << 5 = 00000**010**