brief introduction
Application scenarios
Source code of case
brief introductionThe numbers in the program exist in binary form in the computer memory , Bit operation is directly to the integer in memory corresponding to the binary operation , Generally, the operation is carried out after digitizing into binary numbers .
Application scenariosUse bit operations in general operations and bit operations , Can improve performance . But it will make the code difficult to understand , It is suggested to make rational use of .
1、 Count odd numbers
2、 Statistical even number
3、 Statistics of different numbers, etc
4、 Find the opposite number
Bit operation has 6 Kind of :
1、 Bitwise AND : Both of them are 1 when , The result is 1( Count odd numbers ) I.e. complete 1 by 1.
2、 Press bit or : Both of them are 0 when , The result is 0( Statistical even number ) I.e. complete 0 by 0.
3、 Bitwise XOR : The two bits are the same 0, Dissimilarity is 1( Common statistics are different ) The difference is 1.
4、 According to the not :0 change 1,1 change 0, amount to -x-1
5、 Left shift operation : Each binary moves several bits to the left , High level discard , Low complement 0.
6、 Right shift operation : Each binary moves several bits to the right , To an unsigned number , High compensation 0, Sign digit complement of signed numbers ( Arithmetic shift right ), Or make up 0( Logical shift right ).
Source code of case# -*- coding: utf-8 -*-# time: 2022/5/22 17:56# file: bitwise.py# official account : Play with test development # &: Both of them are 1 when , The result is 1( Count odd numbers ) I.e. complete 1 by 1.a1 = 10b1 = 9"""10 = 0b10109 = 0b10018 = 0b1000"""print(bin(a1))print(bin(b1))print(a1 & b1) # 8print(int("0b1000", 2))# |: Both of them are 0 when , The result is 0( Statistical even number ) I.e. complete 0 by 0.a2 = 10b2 = 9"""10 = 0b10109 = 0b100111 = 0b1011"""print(bin(a2))print(bin(b2))print(a2 | b2) # 11print(int("0b1011", 2))# ^: The two bits are the same 0, Dissimilarity is 1( Common statistics are different ) The difference is 1.a3 = 10b3 = 9"""10 = 0b10109 = 0b10013 = 0b0011"""print(bin(a3))print(bin(b3))print(a3 ^ b3) # 11print(int("0b0011", 2))# ~:0 change 1,1 change 0, amount to -x-1a4 = 10"""10 = 0b1010-x-1 = -11"""print(bin(a4))print(~a4) # -11print(int("-0b1011", 2))# Find the opposite number print(~a4 + 1) # -10# <<: Each binary moves several bits to the left , High level discard , Low complement 0, namely :x << n = x * (2 ** n)a5 = 10"""10 = 0b1010x = 10 * 2 ** 3 = 10 * 2 * 2 * 2"""b5 = a5 << 3print(bin(b5))print(b5) # 80print(int("0b1010000", 2))# >>: Each binary moves several bits to the right , To an unsigned number , High compensation 0, Sign digit complement of signed numbers ( Arithmetic shift right ), Or make up 0( Logical shift right ).# namely :x << n = x / (2 ** n)a6 = 64"""64 = 0b1000000x = 64 / (2 ** 3) = 64 / (2 * 2 * 2)"""b6 = a6 >> 3print(bin(b6))print(b6) # 8# Classic case : Use ^ Find out the number of times a7 = 1 ^ 1 ^ 2a8 = 1 ^ 2 ^ 1a9 = 2 ^ 1 ^ 1print(a7)print(a8)print(a9)# Count the time spent by the original method and the bit operation method import timeloop = 30000000start1 = time.time()odd_list1 = []for i in range(loop): if i & 1 == 1: odd_list1.append(i)end1 = time.time()print(f"time1:{end1 - start1}")start2 = time.time()odd_list2 = []for i in range(loop): if i % 2 == 1: odd_list1.append(i)end2 = time.time()print(f"time2:{end2 - start2}")# time1:5.262001037597656# time2:4.736037492752075
Here is a detailed explanation of Python This is the end of the article on the simple implementation of median operation , More about Python Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !