##### Categories

Bitwise operators

Bitwise operators work with the binary numbers (bits) of integer values. They operate bit by bit, hence they are called bitwise operator. Here is the list of bitwise operators.

 Operator Meaning Example & Bitwise AND x & y | Bitwise OR x | y ~ Bitwise NOT ~x ^ Bitwise XOR x ^ y >> Bitwise right shift x >> 2 << Bitwise left shift x << 2

Bitwise AND

## If both the bits are 1 output is 1. Here is the truth table for bitwise

 Operand1 Operand2 Resultant bit 0 0 0 &  0  = 0 0 1 0  &  1  = 0 1 0 1  &  0  = 0 1 1 1  & 1   = 1

Example : Bitwise AND in Python

print( 2 & 6)

#output 2

## Explanation:

0 0 0 0 0 0 0 1 0 (2)

0 0 0 0 0 0 1 1 0 (6)

---------------------

0 0 0 0 0 0 0 1 0 (2)

A bitwise operator is performed on each bit of the operands. Here each bit of an operand1 is ANDED with a bit of another operand

Bitwise OR

If both the bits are 1 output is 1. Here is the truth table for bitwise

 Operand1 Operand2 Resultant bit 0 0 0  |  0  = 0 0 1 0  |  1  = 1 1 0 1  |  0  = 1 1 1 1 |  1   = 1

Example : Bitwise  OR in Python

print( 2 | 6)

#output 3

Explanation:

0 0 0 0 0 0 0 1 0 (2)

0 0 0 0 0 0 1 1 0 (6)

---------------------

0 0 0 0 0 0 1 1 0 (3)

A bitwise operator is performed on each bit of the operands. Here each bit of an operand1 is ANDED with a bit of another operand

Bitwise Not(~)

## If both the bits are 1 output is 1. Here is the truth table for bitwise

 Operand1 Resultant bit 0 ~ 0 = 1 1 ~1 = 0

Example : Bitwise NOT in Python

print( ~ 2)

#output -3

## Explanation:

0 0 0 0 0 0 0 1 0 (2)

---------------------

1 1 1 1 1 1 1 0 1 (-3)

A bitwise operator is performed on each bit of the operands. Here each bit is reversed for example  0 is converted to 1 and vice-versa. The number is  negative since the sign bit( the left most bit is called a sign bit) is 1.

Bitwise XOR

## If both the bits are same  output is 0 otherwise 1. Here is the truth table for bitwise

 Operand1 Operand2 Resultant bit 0 0 0  |  0  = 0 0 1 0  |  1  = 1 1 0 1  |  0  = 1 1 1 1 |  1   = 0

Example : Bitwise XOR in Python

print( 2 ^ 6)

#output 4

Explanation:

0 0 0 0 0 0 0 1 0 (2)

0 0 0 0 0 0 1 1 0 (6)

---------------------

0 0 0 0 0 0 1 0 0 ( 4)   :: 1 ^ 1 = 0, 0 ^ 1 = 1, 0 ^ 0 = 0

A bitwise operator is performed on each bit of the operands. Here each bit of an operand1 is ANDED with a bit of another operand.

Bitwise Rightshift

The right side bits are removed of the left operand for the given number of times. For example

print(4 >> 1)

#output 2

Explanation:

0000 0100  (4) # Remove  bit form right 1 time

0000 0010 (2)

The rightmost bit is removed new number is become 0000 0010 that is 2.

Bitwise  Left shift

## The left  side bits  moved towards the left of the left operand for the given number of times.For example

print(4 << 1) #Move bit to left 1 time

#output 8

Explanation:

0000 0100  (4)

0000 1000 (8)

The left side  bit  is moved towards the left, hence  the new number is become 0000 1000 that is 8.