Introduction
Values and variables can be operated on using operators. These unique symbols are used to perform logical and arithmetic operations. Operand is the value that the operator operates on.
Bitwise Operators
Bitwise operations on integers are carried out in Python using bitwise operators. The term "bitwise operators" refers to the process of performing operations on individual bits or related pairs of bits after converting the integers into binary. After that, the result is given back in decimal format.
Bitwise "AND" Operator
ð Returns 1 if both the bits are 1, else it returns 0.
x = 10 = 1010 (Binary)
y = 4 = 0100 (Binary)
x & y = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise "OR" Operator
ð Returns 1 if either of the bits is 1, else it returns 0.
x = 10 = 1010 (Binary)
y = 4 = 0100 (Binary)
x | y = 1010
|
0100
= 1110
= 14 (Decimal)
Bitwise "NOT" Operator
ð It returns one's complement of the number.
x = 10 = 1010 (Binary)
Since 32 bits are typically used to represent numbers in computers, the binary representation of 10 is
(....0000 1010)[32 bits]
~x is basically 1's complement of x
i.e ~x should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result
Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.
2's complement of intermediate-result is:
intermediate-res = 0101 //....1111 0101
1010 //....0000 1010 -(1's complement)
+1
-----------
= 1011 //....0000 1011
-----------
= -11 (Decimal)
thus ~x = -11
Bitwise "XOR" Operator
ð Returns 1 if one of the bits is 1 and the other is 0 else returns false
x = 10 = 1010 (Binary)
y = 4 = 0100 (Binary)
x ^ y = 1010
^
0100
= 1110
= 14 (Decimal)
Shift Operators
ð By shifting the bits of a number left or right, these operators can multiply or divide a number by two, accordingly. When we need to divide or multiply a number by two, we can use them.
Bitwise Right Shift
ð Shifts the number's bits to the right and, as a result, fills 0 on voids left (or 1, in the case of a negative integer). Similar results to when the number is divided by a power of two.
Example 1:
x = 10 = 0000 1010 (Binary)
y >> 1 = 0000 0101 = 5
Example 2:
x = -10 = 1111 0110 (Binary)
y >> 1 = 1111 1011 = -5
Bitwise Left Shift
ð Shifts the number's bits to the left and, as a result, fills 0 on the right gaps. A power of two multiplied by the number has a similar effect.
Example 1:
x = 5 = 0000 0101 (Binary)
x << 1 = 0000 1010 = 10
x << 2 = 0001 0100 = 20
Example 2:
y = -10 = 1111 0110 (Binary)
y << 1 = 1110 1100 = -20
y << 2 = 1101 1000 = -40
Bitwise Operator Overloading
ð Giving something more meaning than its intended operational definition is known as "operator overloading." For instance, you can join two strings, combine two lists, and add two integers with the operator +. Because the int and str
classes override the'+' operator, it is possible. Operator overloading is the phenomenon when an identical built-in operator or function behaves differently for objects belonging to distinct classes.
Top comments (0)