### Bitwise Operators | Python

Sometimes we require the manipulation of individual bits within a word of memory. Machine language and Assembly language are normally required for this type of operation. However, python contains several operators that allow such bitwise operation to be carried out in an easy manner, that means these operators are capable of operating on each bit of data.

There are three categories of bitwise operators in python, which are listed below :-

1. Bitwise complement Operator (~)
2. Bitwise logical Operators
3. Bitwise Shift Operator

## Bitwise Complement Operator (~)

The Bitwise complement operator (~)  is a unary operator. It converts the decimal number to its corresponding binary bit value.

That is in case of 5 it first converts 5 to 0000 0101 (to 8 bit binary translation). Then it converts all the 1 in the number to 0, and all the zeros to 1; then the number will become 1111 1010.

Then the 2’s complement representation of this number will be -6.

For example :-

```print(~5)
```

The output will be :-

```-6
```

Note: We can’t apply bitwise complement operators to the floating point numbers.

## Bitwise logical Operators

Like the bitwise complement operator, the bitwise logical operator also works on bit in python. There are three bitwise logical operators in python :

1. Bitwise AND Operator ( & )
2. Bitwise OR Operator   ( | )
3. Bitwise XOR Operator  ( ^ )

### Bitwise AND Operator (&)

A bitwise AND operator takes two values as input and returns a 1 if both bits have a value of 1. It performs logical operation on bit using the following truth table:

A B A  & B
0 0 0
0 1 0
1 0 0
1 1 1

Let’s understand bitwise AND operation through a program :-

``````a = 2
b = 7
print(a & b)``````

The output will be :-

```2
```

### Bitwise OR( | ) Operator

The bitwise OR operator takes two values as input and returns 1 if any of them is 1, else return 0. The truth table is given below:

A B A  | B
0 0 1
0 1 1
1 0 1
1 1 0

Let’s understand this through a program :-

``````a = 3
b = 7
print(a | b)``````

The output of the above program will be :-

```7
```

### Bitwise XOR (^) Operator

The bitwise exclusive OR(XOR) takes two values as input and returns 1 if both the values are different and 0 if both the values are the same.

It performs logical operation on bit using the following truth table:

A B A  ^ B
0 0 0
0 1 1
1 0 1
1 1 0

Let’s understand  bitwise XOR operator through a python program :-

``````a = 12
b = 3
print(a ^ b)``````

The output of the following code is :-

```15
```

## Bitwise Shift Operator

Python provides  two bitwise shift operators for shifting bits left or right . Each operator requires two operands that represent bit value to be shifted and the second is a positive integer that indicates the number of displacements(position).

The number of displacements of bits is known as shift count. The shift count must be a positive integer and less than the number of bits required to represent data of the type of the left operand.

There are two types of bitwise shift operators in python :-

1. Bitwise Right-shift Operator
2. Bitwise Left-shift Operator

### Bitwise Right-Shift Operator (>>)

The right shift operator shifts bits to the right side and inserts 0s from the left end.This operator causes all the bits to be shifted to the right by the number of positions indicated  by the second number.

First number << Second number

The second number represents the number of bit positions that we have to shift to the right side and the shift operation is performed on the first number.

Let’s understand right shift operator through an example -

``````a = 12
b = 2
print(a >> b)`````` The output of the following program after right shifting will be :-

```16
```

If the number is shifted 1 bit, then the right shift operator halves the number and if the number is shifted 2 bit, then the number will be half of half and so on. ### Bitwise left-shift operator( << )

The left shift operator shifts bits to the left side. This operator causes all the bits to be shifted to the left by the number of positions indicated  by the second operand (right side). The leftmost bit in the initial bit pattern will be discarded or lost and the rightmost bit position will be filled with 0. In simple terms the binary number appended at the end of the bi pattern.

First number << Second number

The second number represents the number of bit positions that we have to shift to the right side and the shift operation is performed on the first number.

Let’s understand left shift operator through an example :-

``````a = 12
b = 2
print( a << b)
`````` The output of the program after left shifting the bit will be :-

```48
```

In bitwise left shifting, if we shift 1-bit then the left shift operator doubles the number and if we shift 2-bit then the number will become 4 times the initial number. The former type of operation is known as the arithmetic right and left shifting and the latter type the logical right and left shifting.