Summary of decimal bitwise operations and computation in Python

Source: Internet
Author: User
Bitwise computation is the foundation of computer learning. Python fully supports bitwise operators to display the result of bitwise computation almost directly, here we will briefly summarize the Basic decimal bitwise calculation summary and computation examples in Python. And Operations &

Example:
3 & 5
Solution: The binary complement code of 3 is 11, the complement code of 5 is 101, and the complement code of 3 & 5 is 011 & 101. first look at the hundred bits (actually not a hundred bits, this is just easy to understand) one 0 and one 1. according to (1 & 1 = & 0 = & 0 = & 1 = 0), we can see that bits should be 1, the number 1 & 0 = 0 in the same ten digits, the number 1 & 1 = 1 in a single digit, so the final result is 1. (There should be another step after that, because the value we get now is just the complement code for the answer, but because the positive value is itself, it is omitted. However, the following example cannot omit the last step ).
-1 &-2
Solution: the complement code OF-1 is 11111111, and the complement code OF-2 is 11111110,111 11111 & 11111110. The result is 11111110. this is the complement code, then convert the bitwise original code to 100000010 (the negative conversion bitwise original code is to subtract one to retrieve the inverse), and finally convert it to decimal to-2.
-2 & 6
Solution: the complement code OF-2 is 11111110, and the complement code of 6 is 110,111 11110 & 110, that is, 11111110 & 00000110 (so that beginners can better understand the bitwise operation ), the result obtained according to the above method is: 110, and the conversion decimal is 6.
Tips: use bitwise and to change the last digit of any binary number to 0, that is, X & 0.

Eg:

a = 5b = 3print a & b

Result: 1
How is this calculated? in fact, it is calculated through the binary values of a and B.

# Binary of B of a #0*2 ** 3 + 1*2 ** 2 + 0*2 ** 1 + 1*2 ** 0 # start with Operation a = 0101b = 0011

Result: 0001
Compared with the operation, the binary values of a and B are regarded as 1 if the number of digits is 1, and 0 if they do not want to be the same or both. Then convert the binary value of the answer to a decimal value.

Or operation |
Example:
4 | 7
Solution: The calculation rule of the bitwise sum is similar to that of the bitwise operator, except that the logic operator is changed, and the rule is: 1 | 1 = 1, 1 | 0 = 1, 0 | 0 = 0. 4 | 7 conversion bit binary: 100 | 111 = 111. binary 111 is 7 in decimal format.
Tips: you can use bitwise to change the last bit of any binary number to 1, that is, X | 1.
Eg:

a = 5b = 3print a | b

Result: print 7

a = 0101b = 0011

A | B: 0111
The OR operation is the opposite of the operation. if the number of digits is not 0, the value is 1. Otherwise, the value is 0.


XOR operation
Method: Add pairs. Note that do not carry.
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 cannot carry, so the result is 0)
-3 ^ 4
Solution: the complement code OF-3 is 1111110100 and the complement code is 00000100 (that is, 11111101), 00000100 ^ 11111101 = 11111101. the complement code 1000111 is converted to the original code, that is, decimal-7.

a = 5b = 3print a ^ b

Result: 6

a = 0101b = 0011

The result of a ^ B is 0110.
If you do not want to use the same number of digits, the value is 1. Otherwise, the value is 0.

Move left and right
1. left shift operator <
Method: X < Example:
3 <2
Solution: change the number of two digits to 1100, that is, 12.

2. right moving operator>
Method: X> N: move the binary number corresponding to the number X to the right N bits.
Example:
3> 2
Solution: move the two digits to the right of 11 to 0.
10> 1
Solution: The binary value of 10 is 1010, and the value of moving one digit to the right is 101, that is, 5.

a = 5b = 2print a << b

The result is 20.

a = 0101b = 2

A <B result: 10100
The displacement operation moves the binary number to the left or the right. the preceding operation is to move two units to the left.

For more information about the decimal bitwise calculation and Python computing, see PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.