In python, "and, or, different or" is different from the C language.

Source: Internet
Author: User
In python: bitwise operations are all bitwise operations. they all perform operations in the binary form of the number involved in the calculation. 1. operation: when the values of A and B are both 1, the calculation result of A and B is 1. otherwise, in python:

Bitwise operations are all bitwise operations, which are performed in the binary form of the number involved in the operation.

1. operation: when the values of A and B are both 1, the calculation result of A and B is 1. Otherwise, it is 0 (operator :&)

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

3. exclusive or operation: When A is 1 different from B, the budget result of A and B is 1; otherwise, the value is 0 (operator: ^)

4. flip by bit (reverse by bit): returns the binary number of numbers in the memory in decimal order 0 to and takes 0 (operator :~)

Calculation method:

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

Note: it means to convert all two numbers to binary for comparison. for example, the binary value of 5 is 0011, and the binary value of IS. then the binary numbers of the two numbers have the same first digit, take one, and the rest all take zero. Get 0001, and then convert to decimal result 1

For ease of understanding, here is another example in Table 5-4 of "python core programming": >>> 30 & 45 with 12 results

That is, the bitwise of 30 = (011110) and 45 = (101101) is calculated as 12 (1100)

The other three bitwise operations are the same.

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

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

4. flip by bit (reverse by bit): 5 = 0101 (2) calculation process ~ 5 = 1010 (2) for example, the bitwise operation of X is-(X + 1)

Other bitwise operations: <,>)

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

2. >>: the method of right shifting is the same as that of left shifting, but only in the opposite direction.

Negative number in the computer:

Round back the original number by BiT + 1 = complement the original number (reverse number of the original number)

Original number: in the computer, the binary representation of a common number is the original number.

Example: 5 = 00000000 00000000 00000000 00000101

5. The result is 11111111 11111111 11111111 11111010. The result + 1 indicates the complement of the original number, which is the binary representation of-5.

C language:

The priority ranges from high to low ~ , &, ^, |

Bitwise AND operation have two typical usage: one is to take a few digits of a single bit string information. for example, the following code intercepts the minimum 7 digits of x: x & 0177. Second, let a variable retain a certain number of digits, and the remaining position is 0. for example, the following code keeps x at least 6 digits: x = x & 077.

A typical usage of bitwise OR operations is to set the location of a single bit string to 1. If you want to obtain the rightmost 4 bits as 1, the other bits must be the same as the other bits of the variable j. you can use the logic or operation 017 | j;

A typical usage of bitwise exclusive or operation is to reverse the information of a single bit string. For example, if you want to reverse the information of the rightmost 4 bits of the integer variable j, you can use the logic XOR operation 017 ^ j to reverse the information of the rightmost 4 bits of j, that is, the first bits, the result is 0, and the original value is 0. The result is 1. Exchange two values without temporary variables, for example, a = 3, B = 4. To swap values of a and B, use the following value assignment statement:

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

Inverse operations are often used to generate constants irrelevant to system implementations. To set the minimum position of variable x to 0, the remaining digits remain unchanged, use the code x = x &~ 077.

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.