Home > Backend Development > Python Tutorial > Summary of decimal bitwise operations and calculations in Python

Summary of decimal bitwise operations and calculations in Python

高洛峰
Release: 2017-03-01 14:14:25
Original
1359 people have browsed it

And operation&

Example:
3&5                                                                                                       
Solution: The two’s complement of 3 is 11, and the two’s complement of 5 is 101, 3&5 is 011&101, Let’s look at the hundreds digit first (actually it is not the hundreds digit, this is just to facilitate understanding). There is a 0 and a 1. According to (1&1=1, 1&0=0, 0&0=0, 0&1=0), we know that the hundreds digit should be 1, and the tens digit is also the same. The number 1&0=0, the number in the ones place is 1&1=1, so the final result is 1. (There should be one step after this, because the value we get now is just the complement of the answer we are looking for, but because of the positive number The complement is itself, so it is omitted. However, the last step cannot be omitted in the following example).
-1&-2
Solution: The complement of -1 is 11111111, and the complement of -2 is 11111111. The code is 11111110, 11111111&11111110. The result is: 11111110. This is the complement code. Then the original code is converted to 100000010 (the method of converting a negative number to the original code is to subtract one and negate it). The final conversion to decimal is -2.
-2&6
Solution: -2's complement is 11111110, 6's complement is 110, 11111110&110, which is 11111110&00000110 (the purpose of writing this is to allow beginners to better understand bitwise operations), follow the above method to get The result is: 110, converted to decimal is 6.
Tips: Use bitwise AND to change the last digit of any binary number to 0, which is X&0.

eg:

a = 5
b = 3

print a & b
Copy after login

Result: 1
How is this calculated? It is actually calculated through the binary system of a and b.

# a 的 b 的二进制
# 0*2**3 + 1*2**2 + 0*2**1 + 1*2**0
# 开始与运算
a = 0101
b = 0011
Copy after login

Result: 0001
The AND operation is to compare the binary numbers of a and b. If the digits are both 1, it will be counted as 1. If you don’t want the same or both If it is 0, it is counted as 0. Then convert the answer from binary to decimal.

OR operation|
Example:
4|7
Solution: The calculation rule of bitwise union is very similar to that of bitwise AND, but the logic is changed. operator, the rule of union is: 1|1=1,1|0=1, 0|0=0. 4|7 converted to binary is: 100|111=111. Binary 111 is 7 in decimal.
Tips: Using bitwise addition, you can change the last digit of any binary number to 1, which is X|1.
eg:

a = 5
b = 3

print a | b
Copy after login

Result: print 7

a = 0101
b = 0011
Copy after login
Copy after login

a | bThe result is: 0111
The OR operation is exactly the opposite of the AND operation. If the bit If the number is not 0, it is counted as 1, otherwise it is counted as 0.


XOR operation
Method: Bit addition, special attention should be paid to not carrying. ​
Example:
2^5
Solution: 10^101=111, the decimal result of binary 111 is 7.
1^1
Solution: 1+1=0. (Originally binary 1+1=10, but carry is not allowed, so the result is 0)
-3^4
Solution: The complement of -3 is 11111101, the complement of 4 is 100 (that is, 00000100), 11111101^00000100=11111101, the complement of 11111101 converted to the original code is 1000111, That is -7 in decimal.

a = 5
b = 3

print a ^ b
Copy after login

Result: 6

a = 0101
b = 0011
Copy after login
Copy after login

a ^ b The result is 0110
If the number of digits in the XOR operation is not the same, it is counted as 1, otherwise it is counted as 0.

Left shift and right shift
1. Left shift operator <<
Method: Move N bits to the left.
Example:
3<<2
Solution: 11 moves two places to the left to become 1100, which is 12.

2. Right shift operation Symbol >>
Method: X>>N Move the binary number corresponding to a number :11 moves two places to the right and becomes 0.
10>>1
Solution: The binary number of 10 is 1010, and moving one place to the right is 101, which is 5.



a = 5
b = 2

print a << b
Copy after login

The result is 20

##

a = 0101
b = 2
Copy after login

a << bResult: 10100

The bit shift operation will move the binary number to the left or right. As shown above, it moves 2 units to the left.

For more articles related to summary of decimal bitwise operations and calculations in Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template