Bitwise operations of Python

Source: Internet
Author: User
Tags bitwise

#coding =utf-8
# "&" bitwise AND operation refers to a number converted to binary, then these binary numbers are bitwise
A=7&18
Print a
"' first convert 7 to binary, get 7 binary value is 111, auto complement 8 bit, that is 00000111, then 18 into binary, get 18 binary value is 10010, same complement 8 bit, 00010010.
We have 00000111 and 00010010 bitwise AND operation, the result is 00000010, and then converted to decimal, 2 "


#按位或运算, we want to convert the number to binary after bitwise OR operation
B=7|18
Print B
Print B
The binary of "7" is 00000111,18 binary is 00010010
We have 00000111 and 00010010 bitwise OR operation, the result is 00010111,
Then we convert 00010111 to decimal and get the number 23 "'


# "^" Bitwise XOR
C=7^18
Print C
"' Xor ' is 1, same as 0
7 binary form is the binary form of the 00000111,18 is 00010010
We will make 00000111 and 00010010 bitwise different OR operation
Get the result dead 00010101 and then convert 00010101 to Decimal
Get the number 21 "'

# "~" bitwise reversal ~x=-(X+1)
D=~18 #~18=-(18+1) =-19
Print D


# "<<" shift left
"such as 18 left is to move his binary form 00010010 left, that is, after the shift is called 00100100, that is, 00100100, 36, left one grid unit equivalent to multiply 2, left two units equivalent to multiply 4, left 3 units equivalent to multiply 8.
Shift left N units equal to 2 n power "'
E=18<<1
Print E

F=3<<3
Print F

#coding =utf-8
# "&" bitwise AND operation refers to a number converted to binary, then these binary numbers are bitwise
A=7&18
Print a
"' first convert 7 to binary, get 7 binary value is 111, auto complement 8 bit, that is 00000111, then 18 into binary, get 18 binary value is 10010, same complement 8 bit, 00010010.
We have 00000111 and 00010010 bitwise AND operation, the result is 00000010, and then converted to decimal, 2 "


#按位或运算, we want to convert the number to binary after bitwise OR operation
B=7|18
Print B
Print B
The binary of "7" is 00000111,18 binary is 00010010
We have 00000111 and 00010010 bitwise OR operation, the result is 00010111,
Then we convert 00010111 to decimal and get the number 23 "'


# "^" Bitwise XOR
C=7^18
Print C
"' Xor ' is 1, same as 0
7 binary form is the binary form of the 00000111,18 is 00010010
We will make 00000111 and 00010010 bitwise different OR operation
Get the result dead 00010101 and then convert 00010101 to Decimal
Get the number 21 "'

# "~" bitwise reversal ~x=-(X+1)
D=~18 #~18=-(18+1) =-19
Print D


# "<<" shift left
"such as 18 left is to move his binary form 00010010 left, that is, after the shift is called 00100100, that is, 00100100, 36, left one grid unit equivalent to multiply 2, left two units equivalent to multiply 4, left 3 units equivalent to multiply 8.
Shift left N units equal to 2 n power "'
E=18<<1
Print E

F=3<<3
Print F


# ">>" Move right
The right shift is the inverse of the left shift, that is, the relative binary number moves to the right, one unit to the right is the equivalent of dividing by 2, the right shift of two units is equivalent to dividing by 4, the right shift of 3 units is equal to dividing by 8, the right shift n units is equivalent to dividing by 2 n power ""
G=18>>1
Print g

H=18>>2
Print h

# "<=" is less than or equal to the symbol, comparison operation, less than or equal to, returns a bool value
H=3<=3
Print h

I=4<=3
Print I


# ">="
J=1>=3
Print J

K=4>=3
Print K


# "= =" Compares two objects for equality
L=12==13
Print L

m= "Hello" = = "Hello"
Print m

#not逻辑非
N=true
M=not N
Print m

O=false
Print not O

#and逻辑与
"True and True equals True
True and False equals false
False and True equals Flase ' "

Print true and True

# "or"; logical OR
"True and True equals True
True and False equals True
False and True equals Flase ' "
Print True and False



Bitwise operations of Python

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.