Bitwise operations are very useful operations sometimes .These few operations are necessary in working with device drivers, low-level graphics, cryptography, and network communications.
They alter binary strings at the bit level. They are directly supported by the processor.
Types of the Bitwise operators are :
1. Bitwise OR
2. Bitwise AND
3. Bitwise NOT
4. Bitwise XOR
5. Bitwise Left Shift
6. Bitwise Right Shift
7. Inplace
We are going to see all of them one by one.
1. Bitwise OR
The | operator will perform a binary "or," this operation will return one if any of the bits are one else it will return zero.
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
# So we are going to perform OR operation between 60 and 30
# 60 = 0b111100
# 30 = 0b011110
60 | 30
# Out: 62
# 62 = 0b111110
Bitwise
AND
The & operator will perform a binary AND operation , this operation will return one if both the bits are one else it will return zero.
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
# 60 = 0b111100
# 30 = 0b011110
60 & 30
# Out: 28
# 28 = 0b11100
Bitwise
NOT
The ~ operator will flip all of the bits in the number. Since computers use signed number representations and two's complement notation . This means that if you were using 8 bits to represent your two's-complement numbers, you would treat patterns from 0000 0000 to 0111 1111 to represent numbers from 0 to 127 and reserve 1xxx xxxx to represent negative numbers. Eight-bit two's-complement numbers .
In general, though, this means ~n = -n - 1.
# 0 = 0b0000 0000
~0
# Out: -1
# -1 = 0b1111 1111
# 1 = 0b0000 0001
~1
# Out: -2
# -2 = 1111 1110
# 2 = 0b0000 0010
~2
# Out: -3
Bitwise
XOR (Exclusive OR)
The ^ operator will perform a binary XOR . The XOR operation between two operands will be zero if both of the bits are same otherwise it will be one .
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
# Suppose two numbers are 60 and 30
# 60 = 0b111100
# 30 = 0b011110
60 ^ 30
# Out: 34
# 34 = 0b100010
Bitwise
Left Shift
The << operator will perform a bitwise "left shift," where the left operand's value is moved left by the number of bits
given by the right operand.
# 2 = 0b10
2 << 2 #left shift the binary value of 2 by two places
# Out: 8
# 8 = 0b1000
#Performing a left bit shift of 1 is equivalent to multiplication by 2:
7 << 1
# Out: 14
Performing a left bit shift of n is equivalent to multiplication by 2**n:
3 << 4
# Out: 48
Bitwise
Right Shift
The >> operator will perform a bitwise "right shift," where the left operand's value is moved right by the number of
bits given by the right operand.
# 8 = 0b1000
8 >> 2 #right shift the binary value of 8 by two places
# Out: 2
# 2 = 0b10
#Performing a right bit shift of 1 is equivalent to integer division by 2:
36 >> 1
# Out: 18
15 >> 1
# Out: 7
#Performing a right bit shift of n is equivalent to integer division by 2**n:
48 >> 4
# Out: 3
59 >> 3
# Out: 7
Inplace
Operations
All of the Bitwise operators above(except not) have their own in place versions.
a = 0b001
a &= 0b010
# a = 0b000
a = 0b001
a |= 0b010
# a = 0b011
a = 0b001
a <<= 2
# a = 0b100
a = 0b100
a >>= 2
# a = 0b001
a = 0b101
a ^= 0b011
# a = 0b110