程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python: bit operations

編輯:Python

brief introduction : The 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 scenarios : Use 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 = 10
b1 = 9
""" 10 = 0b1010 9 = 0b1001 8 = 0b1000 """
print(bin(a1))
print(bin(b1))
print(a1 & b1) # 8
print(int("0b1000", 2))
# |: Both of them are 0 when , The result is 0( Statistical even number ) I.e. complete 0 by 0.
a2 = 10
b2 = 9
""" 10 = 0b1010 9 = 0b1001 11 = 0b1011 """
print(bin(a2))
print(bin(b2))
print(a2 | b2) # 11
print(int("0b1011", 2))
# ^: The two bits are the same 0, Dissimilarity is 1( Common statistics are different ) The difference is 1.
a3 = 10
b3 = 9
""" 10 = 0b1010 9 = 0b1001 3 = 0b0011 """
print(bin(a3))
print(bin(b3))
print(a3 ^ b3) # 11
print(int("0b0011", 2))
# ~:0 change 1,1 change 0, amount to -x-1
a4 = 10
""" 10 = 0b1010 -x-1 = -11 """
print(bin(a4))
print(~a4) # -11
print(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 = 0b1010 x = 10 * 2 ** 3 = 10 * 2 * 2 * 2 """
b5 = a5 << 3
print(bin(b5))
print(b5) # 80
print(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 = 0b1000000 x = 64 / (2 ** 3) = 64 / (2 * 2 * 2) """
b6 = a6 >> 3
print(bin(b6))
print(b6) # 8
# Classic case : Use ^ Find out the number of times 
a7 = 1 ^ 1 ^ 2
a8 = 1 ^ 2 ^ 1
a9 = 2 ^ 1 ^ 1
print(a7)
print(a8)
print(a9)
# Count the time spent by the original method and the bit operation method 
import time
loop = 30000000
start1 = 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

WeChat official account : Play with test development
Welcome to your attention , Common progress , thank you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved