You can learn about Operators in Python Programs with Outputs helped you to understand the language better.
Python Programming – Bitwise Operators
A bit is the smallest possible unit of data storage, and it can have only one of the two values : 0 and 1. Bitwise operator works on bits and performs the bit-by-bit operation, instead of whole. Table 4.4 shows the bitwise operators and their meaning.
Table 4.4 Bitwise Operators and their Meaning
Bitwise operator | Meaning |
>> | Right hand |
<< | Left hand |
& | AND |
I | OR |
∧ | XOR |
˜ | One’s complement |
- Python Programming – Operators And Their Precedence and Associativity
- Python Programming – Operators
- Python Programming – Expressions
In some other languages, A is used for exponentiation, but in Python, it is a bitwise operator called xor.
In order to understand the bitwise operators, let us first consider the SHIFT operators. There are two types of shift operators that shift the bits in an integer variable by a specified number of positions. The ‘»’ operator shifts bits to the right, and the ‘«’ operator shifts bits to the left.
Right Shift Operator ‘>>’
The right shift operator (>>) shifts the bits of the number to the right by the number of bits specified. This means that those many bits are lost.
In general, the right shift operator is used in the form:
variable >> number-of-bits
11 >> 1 gives 5. 11 is represented in bits by 1011 which when right-shifted by 1-bit gives 101, which is the decimal 5. Take another example; you know that the decimal number 8 is represented in binary form as:
1000
Similarly, the decimal number 4 is represented as:
100
If data is represented in a computer using 8 bits form, then decimal digits 8 and 4 will be represented by 00001000 and 00000100, respectively. The only difference between these two numbers is that the “1” in the second number is shifted one bit to your right. If the variable “a” holds the value “8”, then you can shift it right by one position, with the following statement:
a >> 1
This expression when converted to decimal value would be evaluated as “4”. You can shift a number by several bits. For example, you can change “8” into “2” by shifting the digit’T by two bits to the right by using the expression:
a >> 2
Left Shift Operator ‘ << ‘
The left shift (<<) operator shifts bits of the number to the left for a specified number of bit positions. This means it adds Os to the empty least-significant places. Each number is represented in memory by bits or binary digits, i.e., 0 and 1. The expression takes the form:
variable << number-of-bits
2 << 2 gives 8.
2 is represented by 10 in bits. Left shifting by 2 bits gives 1000 which represents the decimal 8.
Example 15.
Assume a = 60, and b = 11; Now in binary format they will be as follows:
a = 0011 1100, b = 0000 1011
Solve the following bitwise operators a>>2, a<<2, b>>2 and b<<2.
Solution:
The left operand value is moved right by the number of bits specified by the right operand,
a >> 2 = 15
(means 0000 1111)
The left operands value is moved left by the number of bits specified by the right operand,
a << 2 = 240
(means 1111 0000)
Same as above; the left operand value is moved right by the number of bits specified by the right operand,
b >> 2 = 2
(means 0000 0010)
The left operands value is moved left by the number of bits specified by the right operand,
b << 2 = 44
(means 0010 1100) See Figure 4.13
Let’s Try
Assume a = 60, and b = 13; Now in binary format they will be as follows: a = 0011 1100 b = 0000 1101 Write the output of the following bit-wise operations: Find a>>2, a<<2, b>>2, b<<2. |
The bin( ) method converts and returns the binary equivalent string of a given integer.
& (Bitwise AND)
Bitwise AND returns 1 if both the bits are 1, otherwise 0. For example, numbers 5 & 9 give 1.
| (Bitwise OR)
Bitwise OR returns 1 if any of the bits is 1. If both the bits are 0, then it returns 0. For example, numbers 5 I 9 give 13.
A (Bitwise XOR)
Bitwise XOR returns 1 if one of the bits is 0 and the other bit is 1. If both the bits are 0 or 1, then it returns 0. For example, the number 5 A 9 gives 12.
~ (Bitwise invert)
The bitwise inversion of numbers ~5 gives -6 and ~9 gives -10. (See Figure 4.14).
Example 16.
Assume a = 60, and b = 11; now in the binary format, they will be as follows:
a = 0011 1100
b = 0000 1011
Solve the following bitwise operations:
a&b, a I b, aAb, ~a, and ~b.
Solution:
a&b = 0000 1000 →8 # Operator copies a bit to the result if it exists in both
a I b = 0011 1111 →63 # It copies a bit if it exists in either operand.
a∧b = 0011 0111 →55 # It copies the bit if it is set in one operand but not both.
~a = -0011 1101 →-61 # It returns complement of a number.
~b = -0000 1100 →-12 # It returns complement of a number.
Let’s Try
Write the output of the following bitwise operations:
Assume if a = 60, and b = 13; Now in the binary format they will be as follows: a = 0011 1100 b = 0000 1101 Find a&b, alb, a*b, -a, ~b. |