Factors that are used to deal with bits (Bitwise Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| Bitwise AND | & | a & b | The operator & calculates the product of the sum of bits common between a and b |
| Bitwise OR | | | a | b | The operator | calculates the product of the sum of bits Common and unshared between a and b |
| Bitwise XOR | ^ | a ^ b | The operator ^ calculates the product of the sum of bits unshared between a and b |
| Bitwise compliment OR | ~ | ~a | The worker ~ turns the bits which equals 0 to 1 and inverts the bits Which equals 1 to 0 , then add 1 to them and calculate their sum, then multiply the answer by the factor ( - ) Then he gives us a negative answer. |
| left shift | << | a << 2 | The factor << displaces the bits From the last left to the first right. The number 2 means that we will remove the last two bits And we put them first. |
| Right shift | >> | a >> 2 | The factor >> displaces the bits From the first right to the last left. The number 2 means that we will remove the first two bits And we put them last. |
Here we put an example for each factor in the table
Examples of factors that are used to deal with Bits in python
worker & (Bitwize AND)
The operator & calculates the product of the sum of bits commonality between two values.
Example
a = 10 # a = 10 = 00000000000000000000000000001010 b = 75 # 7b = 75 = 00000000000000000000000001001011 c = a & b # We explained how the output will be obtained under the run result print(a, '&', b, '=', c)
• We will get the following result when running.
• Here we have taught the bits Common and collected in yellow.
b = 75; // 75 = 0000000000000000000000000100 1 0 1 1
c = a & b; // c = 0000000000000000000000000000 1 0 1 0
// c = 10
worker | (Bitwize OR)
The operator | calculates the product of the sum of bits The common and the unshared between two values.
Example
a = 10 # 10 = 00000000000000000000000001010 b = 75 #75 = 00000000000000000000000001001011 c = a | b# We explained how the output will be obtained under the run result print(a, '|', b, '=', c)
• We will get the following result when running.
• Here we have taught the bits Common and unshared and collected in yellow.
b = 75; // 75 = 0000000000000000000000000 1 00 1 0 11
c = a | b; // c = 0000000000000000000000000 1 00 1 0 11
// c = 75
worker ^ (Bitwize XOR)
The operator ^ calculates the product of the sum of bits Not shared by two values.
Example
a = 10 # 10 = 00000000000000000000000001010 b = 75 #75 = 00000000000000000000000001001011 c = a ^ b # We explained how the output will be obtained under the run result print(a, '^', b, '=', c)
• We will get the following result when running.
• Here we have taught the bits Unshared and collected in yellow.
b = 75; // 75 = 0000000000000000000000000 1 00101 1
c = a | b; // c = 0000000000000000000000000 1 00000 1
// c = 65
worker ~ (Bitwize Compliment OR)
The worker ~ turns the bits which equals 0 to 1 and inverts the bits which equals 1 to 0 .
Then the output is calculated according to the principle single precision floating point number.
Example
a = 10 # 10 = 00000000000000000000000001010
c = ~a # c = 11111111111111111111111111111110111 = -11
print('~', a, '=', c)
• We will get the following result when running.
worker << (Left Shift)
worker << wipes bits from the left side and then switch all bit It was erased from them with a zero and puts them on the right.
Example
a = 10 # 10 = 00000000000000000000000001010 c = a << 2 # We explained how the output will be obtained under the operation result print(a, '<< 2 =', c)
• We will get the following result when running.
• Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.
c = a << 2; // c = 000000000000000000000000001010 00 = 40
// c = 40
worker >> (Right Shift)
The factor >> has two states: the number may be greater than zero or less than zero.
If the number is greater than zero, erase bits on the right, then switch each bit them with zero and puts them on the left.
If the number is less than zero, erase bits on the right, then switch each bit One of them to keep the minus sign and put them to the left.
first example
• First case: If the number is greater than zero.
a = 9 #9 = 00000000000000000000000000001001 c = a >> 2 # We explained how the output will be obtained under the operation result print(a, '>> 2 =', c)
• We will get the following result when running.
• Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.
c = a >> 2; // c = 00 000000000000000000000000000010
// c = 2
second example
• The second case: If the number is less than zero.
a = -9 # -9 = 111111111111111111111111111111000 c = a >> 2 # We explained how the output will be obtained under the operation result print(a, '>> 2 =', c)
• We will get the following result when running.
• Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.
c = a >> 2; // c = 11 111111111111111111111111111101
// c = -3