Python "with, or, XOR" differs from C language

Source: Internet
Author: User
In Python:

Bitwise operations, both bitwise operations, are the binary forms of the number of participating operations.

1. With Operation: A and B values are 1 o'clock, A, B and the result of the operation is 1, otherwise 0 (operator:&)

2. Or operation: A or B value is 1 o'clock, a, B, or the result of the operation is 1, otherwise 0 (operator: |)

3. XOR operation: A and B are different from 1 o'clock, a, b budget result is 1, otherwise 0 (operator: ^)

4. Bitwise rollover (bitwise REVERSED): 2 binary numbers representing numbers in memory are reversed 0 takes 1, 1 takes 0 (operator: ~)

Operation mode:

1. With operation: 5&3 calculation process is 0101 (2) &0011 (2) =0001 (2) =1

Note: The meaning is to convert all two numbers to binary for comparison, as in the example above: 5 binary for 0101,3 is 0011. Then the first digit of the two number of binary numbers is the same, take one, and all the rest take 0. 0001, then converted to a decimal result of 1

For the convenience of understanding for another example, in the Python core programming example in table 5-4: >>>30&45 result is 12

That is, 30 = (011110), 45 = (101101) bit operation is 12 (1100)

The remaining three bit operations are the same

2. Or operation: The 5|3 calculation process is 0101 (2) |0011 (2) =0111 (2) =7

3. XOR operation: 5^3 calculation process is 0101 (2) ^0011 (2) =0110 (2) =6

4. Bitwise rollover (bitwise negation): 5=0101 (2) Calculation procedure ~5=1010 (2) For example: The bitwise operation of X is-(x+1)

Operation of other bits: (<<,>>)

1. <<: Shift left to move a bit to the left a certain number for example: 2=0010 (2) 2<<2=8 (shift left 2 bit, 1000 (2))

2. >>: The right shift is the same as the left shift only in the opposite direction.

A negative number in the computer indicates:

The complement of the original number of +1= (the inverse number of the original number) by the number of digits reversed

Original number: The number that is formed by using a 2 binary representation of a normal number in a computer is the original number.

Example: 5=00000000 00000000 00000000 00000101

5 Inverse is 11111111 11111111 11111111 11111010 The result of +1 is the complement of the original number is 5 of the system of the expression

In the C language:

Priority levels are high to low, followed by ~, &, ^, |

There are two typical uses of bitwise and operations, one is to take a certain number of bits of information, such as the following code to intercept the minimum 7 bits of x: X & 0177. The other is to let a certain variable hold a certain number of bits, the remaining position 0, such as the following code to keep x only the lowest 6 bits: x = x & 077.

A typical use of a bitwise OR operation is to place a bit string information at a certain location of 1. If you are going to get the right 4 bits to 1, the other bits remain the same as the other bits of the variable J, available logic or operation 017|j;

A typical use of bitwise XOR is to find the inverse of a certain number of bits of information in a bit string. If the desired integer variable J of the right 4 bits of the inverse, with a logical XOR operation 017^j, can be obtained J Right 4 bits of information, that is, the original 1 bit, the result is 0, the original 0 bit, the result is 1. Exchange two values without a temporary variable, if a=3,b=4. To swap the values of a and B, you can use the following assignment statements:

A=a^b; B=b^a; A=a^b;

Inverse operations are often used to generate constants unrelated to the implementation of the system. To make the variable x 6 position 0, the remaining bits will be the same, the code x = x & ~077 can be implemented.

  • 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.