Python shift operator and python Operator

Source: Internet
Author: User

Python shift operator and python Operator

1. Binary

>>> bin( 1 )'0b1'>>> bin( 10 )'0b1010'>>> a = 0b10>>> a2>>> 

2. Shift Operator (>><): The Arrow shifts to the left. The Arrow shifts to the right.

For example, in decimal 1 ---> the binary value is 0000 0001.

1 <1: Move 1 to the left

0000 0001 ---> 0000 0010 (2)

1 <2: Move two places to the left

0000 0001 ---> 0000 0100 (4)

2 <2: 2: Move two places to the left

0000 0010 ---> 0000 1000 (8)

>>> 1 << 12>>> 1 << 24>>> 2 << 28>>> 

Moving to the right is the same principle. First, convert the number to binary, and then move the corresponding number of digits to the right.

>>> 1 >> 10>>> 2 >> 11>>> 3 >> 11>>> 6 >> 13>>> 

6 (0000 0110) ----> after (6> 1) 0000 0011 (3)

 

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 calculation result of A and B is 1. Otherwise, it 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 :~)

 

>>> 1 & 11>>> 1 & 00>>> 4 & 10>>> 
>>> 1 | 11>>> 1 | 01>>> 4 | 15>>> 
>>> 1 ^ 10>>> 7 ^ 815>>> 
>>> ~5-6>>> ~20-21>>> 

 

Related Article

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.