Today in this article we will talk about python bit operators among python operators. Although it is not very commonly used, it still needs to be practiced. I hope this article can help you reading.
Python bitwise operators: Bitwise operators treat numbers as binary to perform calculations.
The bitwise operation rules in Python are as follows: in the following table, variable a is 60, b is 13, and the binary format is as follows:
a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011
Some commonly used symbols are as follows:
After looking at the commonly used symbols, let’s try it. The following example demonstrates the operation of all bit operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "1 - c 的值为:", c c = a | b; # 61 = 0011 1101 print "2 - c 的值为:", c c = a ^ b; # 49 = 0011 0001 print "3 - c 的值为:", c c = ~a; # -61 = 1100 0011 print "4 - c 的值为:", c c = a << 2; # 240 = 1111 0000 print "5 - c 的值为:", c c = a >> 2; # 15 = 0000 1111 print "6 - c 的值为:", c
The values output by the above example are as follows:
1 - c 的值为: 12 2 - c 的值为: 61 3 - c 的值为: 49 4 - c 的值为: -61 5 - c 的值为: 240 6 - c 的值为: 15
The above content is about the bit operators among python operators. This section may be difficult to understand. It is recommended to try it yourself. I hope this article can be helpful to you who are learning python.
The above is the detailed content of python operators - practical application and in-depth analysis of bit operators. For more information, please follow other related articles on the PHP Chinese website!