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 :

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

0 | 1 |

1 | 0 |

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

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

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

### Samples of AND Operator

A = 10001001B = 01011011A & 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 :

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.

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

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

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

### Samples of XOR Operator

A =10001001 B =01011011 A ^ B =11010010

A =11000011B =01011010A ^ B =10011001

A =11001100B =00001111A ^ 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 =0000000100000001<< 1 =0000001000000001<< 2 =0000010000000001<< 5 =00100000

10001001<< 0 =1000100110001001<< 1 =0001001010001001<< 2 =0010010010001001<< 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 =1000000010000000<< 1 = 1100000010000000<< 2 = 1110000010000000<< 5 = 11111100

01000000<< 0 =0100000001000000<< 1 = 0010000001000000<< 2 = 0001000001000000<< 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.