Python bitwise operators -- detailed explanation

Python bitwise operators -- detailed explanation

  • Python Bit operation operates according to the binary bits (bits) of data in memory. It is generally used for underlying development (algorithm design, driver, image processing, single chip microcomputer, etc.), which is not common in application layer development (Web development, Linux operation and maintenance, etc.). Readers who want to speed up their learning or don't pay attention to the underlying development can skip this section first and learn again if necessary in the future.

Python bitwise operators can only be used to manipulate integer types. They are calculated according to the binary form of integers in memory. The bitwise operators supported by Python are shown in Table 1.

&Bitwise and operator

  • The operation rule of bitwise and operator & is: the result is 1 only when the two bits involved in the operation of & are 1, otherwise it is 0. For ex amp le, 1 & 1 is 1, 0 & 0 is 0, and 1 & 0 is 0, which is very similar to the logical operator & &.

For example, 9&5 it can be converted into the following operations:

  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
& 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0001  (1 Storage in memory)

&The operator will & operate all the binary bits of the two integers involved in the operation, and the result of 9 & 5 is 1.

As another example, -9&5 it can be converted into the following operations:

  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
& 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)

-The result of 9-5 is 5.

Again, the & operator operates on the original binary bits of the data stored in memory, not the binary form of the data itself; The same is true for other bitwise operators.
Take - 9 & 5 as an example, the storage of - 9 in memory is quite different from the binary form of - 9:

 1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
-0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (-9 In binary form, the redundant 0 in front can be erased)

Bitwise and operations are usually used to clear 0 for some bits or reserve some bits. For example, to clear the upper 16 bits of n to 0 and reserve the lower 16 bits, you can perform n & 0xfff operation (the storage form of 0xfff in memory is 0000 0000 – 0000 0000 – 1111 1111 1111).

Verify the above analysis with Python code:

n = 0X8FA6002D
print("%X" % (9&5) )
print("%X" % (-9&5) )
print("%X" % (n&0XFFFF) )

Operation results:

1
5
2D

|Bitwise OR operator

  • The operation rule of bitwise OR operator | is: when one of the two binary bits is 1, the result is 1, and when both are 0, the result is 0. For example, 1 | 1 is 1, 0 | 0 is 0, and 1 | 0 is 1, which is very similar to | in logical operation.

For example, 9 | 5 can be converted into the following operations:

  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
| 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1101  (13 Storage in memory)

The result of 9 | 5 is 13.

For another example, - 9 | 5 can be converted into the following operations:

  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
| 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)

-The result of 9 | 5 is - 9.

Bitwise or operations can be used to set some positions to 1, or to retain some bits. For example, if you want to keep the high 16 position 1 of N and the low 16 bits, you can perform the n | 0xfff0000 operation (the storage form of 0xfff0000 in memory is 1111 1111 – 1111 1111 – 0000 0000 – 0000).

Verify the above analysis with Python code:

n = 0X2D
print("%X" % (9|5) )
print("%X" % (-9|5) )
print("%X" % (n|0XFFFF0000) )

Operation results:

D
-9
FFFF002D

^Bitwise exclusive or operator

  • The operation rule of bitwise XOR operation ^ is: if the two binary bits involved in the operation are different, the result is 1, and if they are the same, the result is 0. For example, 0 ^ 1 is 1, 0 ^ 0 is 0, and 1 ^ 1 is 0.

For example, 9 ^ 5 can be converted into the following operations:

  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
^ 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1100  (12 Storage in memory)

The result of 9 ^ 5 is 12.

For another example, - 9 ^ 5 can be converted into the following operations:

  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
^ 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0101  (5 Storage in memory)
-----------------------------------------------------------------------------------
  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0010  (-14 Storage in memory)

-The result of 9 ^ 5 is - 14.

Bitwise XOR can be used to invert some binary bits. For example, to reverse the upper 16 bits of N and reserve the lower 16 bits, you can perform the n ^ 0xfff0000 operation (the storage form of 0xfff0000 in memory is 1111 1111 – 1111 1111 – 0000 0000 – 0000 0000).

Verify the above analysis with Python code:

n = 0X0A07002D
print("%X" % (9^5) )
print("%X" % (-9^5) )
print("%X" % (n^0XFFFF0000) )

Operation results:

C
-E
F5F8002D

~Bitwise negation operator

  • The bitwise negation operator is a unary operator (only one operand), which is right associative. It is used to negate the binary bits involved in the operation. For example, 1 is 0 and ~ 0 is 1, which is the same as that in logical operation! Very similar.

For example, ~9 can be converted to the following operations:

~ 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
-----------------------------------------------------------------------------------
  1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0110  (-10 Storage in memory)

So the result of ~9 is - 10.

For example, ~-9 can be converted to the following operations:

~ 1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
-----------------------------------------------------------------------------------
  0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1000  (8 Storage in memory)

So the result of ~-9 is 8.

Verify the above analysis with Python code:

print("%X" % (~9) )
print("%X" % (~-9) )

Operation results:

-A
8

< < shift left operator

  • Python shift left operator < < is used to shift all binary bits of the operand to the left by several bits, discard the high bits and fill the low bits with 0.

For example, 9 < < 3 can be converted to the following operations:

<< 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
-----------------------------------------------------------------------------------
   0000 0000 -- 0000 0000 -- 0000 0000 -- 0100 1000  (72 Storage in memory)

So the result of 9 < < 3 is 72.

For another example, (- 9) < < 3 can be converted into the following operations:

<< 1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
-----------------------------------------------------------------------------------
   1111 1111 -- 1111 1111 -- 1111 1111 -- 1011 1000  (-72 Storage in memory)

So (- 9) < < 3 results in - 72

If the data is small and the high bit discarded does not contain 1, shifting n bits to the left is equivalent to multiplying by 2 to the nth power.

Verify the above analysis with Python code:

print("%X" % (9<<3) )
print("%X" % ((-9)<<3) )

Operation results:

48
-48

>>Shift right operator

  • Python shift right operator > > is used to shift all binary bits of the operand to the right by several bits, discard the low bits, and fill the high bits with 0 or 1. If the highest bit of the data is 0, fill in 0; If the highest order is 1, make up 1.

For example, 9 > > 3 can be converted to the following operations:

>> 0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 1001  (9 Storage in memory)
-----------------------------------------------------------------------------------
   0000 0000 -- 0000 0000 -- 0000 0000 -- 0000 0001  (1 Storage in memory)

So the result of 9 > > 3 is 1.

For another example, (- 9) > > 3 can be converted to the following operations:

>> 1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 0111  (-9 Storage in memory)
-----------------------------------------------------------------------------------
   1111 1111 -- 1111 1111 -- 1111 1111 -- 1111 1110  (-2 Storage in memory)

So the result of (- 9) > > 3 is - 2

If the discarded low bit does not contain 1, shifting n bits to the right is equivalent to dividing by 2 to the nth power (but the removed bits often contain 1).

Verify the above analysis with Python code:

print("%X" % (9>>3) )
print("%X" % ((-9)>>3) )

Operation results:

1
-2

Keywords: Python

Added by onewaylife on Wed, 02 Feb 2022 01:21:25 +0200