# Bitwise Operators in Python

The following are the bitwise operations that can be applied in Python. The following shifting and bitwise operators can only be applied to integers and long integers:

Operation                                                             Description

x << y                                                                          Left Shift

x >> y                                                                          Right Shift

x & y                                                                            Bitwise AND operation

x | y                                                                              Bitwise OR operation

x ^ y                                                                             Bitwise XOR operation

~x                                                                                  Negation, one's complement.

Bitwise operators assume that the integers are represented in a 2's complement binary representation. For long integers, the bitwise operators operate as if the sign bit is infinitely extended to the left. Some care is required if you are working with raw bit-patterns that are intended to map to native integers on the hardware. This is because Python does not truncate the bits or allow values to overflow - instead, a result is promoted to a long integer.

Let us now see the practical implementation of bitwise operators through an example,

```a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0

c = a & b;        # 12 = 0000 1100

c = a | b;        # 61 = 0011 1101

c = a ^ b;        # 49 = 0011 0001

c = ~a;           # -61 = 1100 0011

c = a << 2;       # 240 = 1111 0000

c = a >> 2;       # 15 = 0000 1111
Or another example,a= 50              # 110010 b=25              #011001c = a & b       # 01000print(c)Output:16```

### Relevant exercises

POST A NEW COMMENT

• Input (stdin)

Output (stdout)

Input (stdin)

Expected Output

Compiler Message

Input (stdin)

`2    3`

`5`
`5`
`5`