A summary of decimal bitwise operations and calculation in Python

Source: Internet
Author: User
and Operations &

Example:
3&5
Solution: 3 of the twos complement is 11, 5 is 101, 3&5 is 011&101, first look at the hundred (actually not the hundred, so it is easy to understand) a 1 1, according to (1&1=1,1&0=0,0&0=0,0 &1=0) that the hundred should be 1, the same as the number 1&0=0, the number on the single digit 1&1=1, so the final result is 1. (It should have been a step later, because the value we get now is just the complement of the answer, but because the positive complement is itself, it's omitted.) However, the following example cannot omit this last step).
-1&-2
Solution:-1 of the complement is 11111111,-2 of the complement is 11111110, 11111111&11111110 obtained the result is: 11111110, this is the complement, and then the conversion of the original code 100000010 (negative conversion of the original code method is minus a negation), The last conversion to decimal is-2.
-2&6
Solution:-2 of the complement is 11111110, 6 of the complement is a, 11111110&110, that is, 11111110&00000110 (so that the purpose of writing is to allow beginners to better understand the bitwise operation), according to the above method to obtain the result is: 110, Conversion bit decimal is 6.
Tip: Use bitwise AND to change the last bit of any binary number to 0, which is x&0.

eg

A = 5b = 3print A & B

Results: 1
This is how to calculate, in fact, through a and B binary calculation.

# a B's binary # 0*2**3 + 1*2**2 + 0*2**1 + 1*2**0# start with operation a = 0101B = 0011

Results: 0001
And the operation is to compare the binary of A and b if the number of bits is 1 is counted as 1, if you do not want the same or both 0 is counted as 0. The binary of the answer is then converted to 10 binary.

or arithmetic |
Example:
4|7
Solution: The calculation of the bitwise AND and the bitwise and is very similar, but changed the logical operator, and the law is: 1|1=1, 1 |0=1, 0|0=0. 4|7 conversion bit binary is: 100|111=111. Binary 111 is the decimal 7.
Tip: Use a bitwise and can change the last digit of any binary number to 1, which is x|1.
eg

A = 5b = 3print A | B

Result: Print 7

A = 0101b = 0011

A | b result is: 0111
Or the operation is exactly the opposite of the operation, if the number of bits is not 0 is counted as 1, otherwise the count is 0.


XOR or operation
Methods: Add to the bits, especially to note that no rounding is needed.
Example:
2^5
Solution: 10^101=111, the binary 111 gets the decimal result is 7.
1^1
Solution: 1+1=0. (originally binary 1+1=10, but cannot carry, so the result is 0)
-3^4
Solution:-3 of the complement is 11111101,4 's complement is 100 (also that is 00000100), 11111101^00000100=11111101, complement 11111101 to the original code is 1000111, that is, the decimal -7.

A = 5b = 3print a ^ b

Results: 6

A = 0101b = 0011

A ^ b turns out to be 0110.
XOR operation is the number of digits that do not want to be counted as 1, otherwise the count is 0.

Move left and right
1. Left shift operator <<
Method: X<<n moves the binary number corresponding to a number X to the left N bits.
Example:
3<<2
Solution: 11 move left two bits to 1100, or 12.

2. Right-shift operator >>
Method: X>>n moves the binary number corresponding to a number X to the right N bits.
Example:
3>>2
Solution: 11 Move right two bits to 0.
10>>1
Solution: 10 of the binary is 1010, moving to the right one is 101, that is, 5.

A = 5b = 2print a << b

The result is 20.

A = 0101B = 2

A << b results: 10100
The displacement operation moves the binary number left or right, as on the left 2 units.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.