Home  >  Article  >  Backend Development  >  The difference between "AND, OR, XOR" in python and C language

The difference between "AND, OR, XOR" in python and C language

高洛峰
高洛峰Original
2016-10-19 13:32:521618browse

In python:

Bitwise operations are all performed on the binary form of the numbers participating in the operation.

1. AND operation: When the values ​​​​of A and B are both 1, the result of the operation of A, B and is 1, otherwise it is 0 (operator: &)

2. OR operation: when the value of A or B is 1 , the operation result of A, B or is 1, otherwise it is 0 (operator: |)

3. XOR operation: only when A and B are different to 1, the budget result of A and B is 1, otherwise it is 0 (Operator: ^)

4. Bitwise flip (bitwise negation): Invert the binary number representing the number in the memory. 0 becomes 1 and 1 becomes 0 (Operator: ~)

Operation method:

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

Note: It means converting both numbers into binary for comparison, as in the above example: the binary of 5 is The binary system of 0101,3 is 0011. Then the first bit of the binary system of these two numbers is the same, which is one, and the rest are all zeros. Get 0001, and then convert it to decimal and the result is 1

For the convenience of understanding, let’s take another example, the example in Table 5-4 in "Python Core Programming": >>>30&45 The result is 12

that is, 30 = (011110), 45 = (101101) bit operation results in 12 (1100)

The remaining three bit operations are the same as this

2. Or operation: 5|3 The 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 flip (bitwise negation): 5=0101(2 ) Calculation process~5=1010(2) For example: the bitwise operation of Move a certain amount to the left, for example: 2=0010(2) 2

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

In computers The negative number representation:

After inverting the original number bitwise + 1 = the complement of the original number (the inverse of the original number)

Original number: represented by a binary system in the computer to form an ordinary number The number is the original number

For example: 5=00000000 00000000 00000000 00000101

5 is the complement of 11111111 11111111 11111111 11111010 The binary representation of -5

in C language :

The priorities from high to low are ~, &, ^, |

There are two typical uses of bitwise AND operation. One is to take certain bits of a bit string information. For example, the following code intercepts the lowest bit of x 7 bits: x & 0177. The second is to allow a variable to retain certain bits and leave the rest at 0. For example, the following code allows x to retain only the lowest 6 bits: x = x & 077.

The typical use of bitwise OR operation is to set certain bits of a bit string information to 1. If you want to obtain the rightmost 4 bits as 1, and the other bits remain the same as the original bits of variable j, you can use the logical OR operation 017|j;

The typical use of bitwise XOR operation is to find certain bits of information in a bit string information The opposite. If you want to find the inverse of the rightmost 4 bits of information of integer variable j, use the logical XOR operation 017^j to find the inverse of the rightmost 4 bits of information of j, that is, the bits that were originally 1, the result is 0, and the original For bits that are 0, the result is 1. Exchange two values ​​without using temporary variables, if a=3, b=4. If you want to interchange the values ​​​​of a and b, you can use the following assignment statement:

a=a^b; b=b^a; a=a^b;

The inversion operation is often used to generate constant. If you want to set the lowest 6 bits of variable x to 0 and leave the remaining bits unchanged, you can use the code x = x & ~077 to achieve this.

Statement:
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
Previous article:Python special syntaxNext article:Python special syntax