Python bitwise 操作员用于对整数进行 bitwise 计算。整数被转换为二进制格式,然后操作被逐步执行,因此被称为 bitwise 操作员。
Python Bitwise 操作员
在Python中有6个bitwise操作员,下表提供了关于它们的简短细节。
Bitwise Operator | Description | Simple Example |
---|---|---|
& | Bitwise AND Operator | 10 & 7 = 2 |
Bitwise OR Operator | ||
^ | Bitwise XOR Operator | 10 ^ 7 = 13 |
~ | Bitwise Ones' Compliment Operator | ~10 = -11 |
<< | Bitwise Left Shift operator | 10<<2 = 40 |
>> | Bitwise Right Shift Operator | 10>>1 = 5 |
让我们看看这些操作员一个接一个,并了解他们是如何工作的。
一、Bitwise和操作员
Python bitwise 和操作员返回 1 如果两个位是 1,否则0。
1>>> 10&7
22
3>>>
二、Bitwise 或操作员
Python bitwise 或操作员返回 1 如果其中任何一个位是 1 如果两个位都是 0,那么它返回 0。
1>>> 10|7
215
3>>>
3、Bitwise XOR操作员
Python bitwise XOR 操作员返回 1 如果其中一个位是 0 和另一个位是 1. 如果两个位是 0 或 1,则返回 0。
1>>> 10^7
213
3>>>
Bitwise Ones的补充操作员
Python Ones 对一个数的补充A
等于 -(A+1)。
1>>> ~10
2-11
3>>> ~-10
49
5>>>
Bitwise Left Shift 操作员
Python bitwise left shift 操作员将左操作员位移动到左侧,在右操作员中给定的次数。
1>>> 10 << 2
240
3>>>
Bitwise Right Shift 操作员
Python的右转操作员是左转操作员的恰恰相反,然后左侧操作员和位被移动到右侧给定的次数。
1>>> 10 >> 2
22
3>>>
Python Bitwise 操作员过载
Python 支持操作员过载. 有各种方法,我们可以实现支持我们自定义对象的 bitwise 操作员。
Bitwise Operator | Method to Implement |
---|---|
& | and(self, other) |
^ | xor(self, other) |
~ | invert(self) |
<< | lshift(self, other) |
>> | rshift(self, other) |
以下是对我们自定义对象的比特方式操作员的过载的例子。
1class Data:
2 id = 0
3
4 def __init__(self, i):
5 self.id = i
6
7 def __and__(self, other):
8 print('Bitwise AND operator overloaded')
9 if isinstance(other, Data):
10 return Data(self.id & other.id)
11 else:
12 raise ValueError('Argument must be object of Data')
13
14 def __or__(self, other):
15 print('Bitwise OR operator overloaded')
16 if isinstance(other, Data):
17 return Data(self.id | other.id)
18 else:
19 raise ValueError('Argument must be object of Data')
20
21 def __xor__(self, other):
22 print('Bitwise XOR operator overloaded')
23 if isinstance(other, Data):
24 return Data(self.id ^ other.id)
25 else:
26 raise ValueError('Argument must be object of Data')
27
28 def __lshift__(self, other):
29 print('Bitwise Left Shift operator overloaded')
30 if isinstance(other, int):
31 return Data(self.id << other)
32 else:
33 raise ValueError('Argument must be integer')
34
35 def __rshift__(self, other):
36 print('Bitwise Right Shift operator overloaded')
37 if isinstance(other, int):
38 return Data(self.id >> other)
39 else:
40 raise ValueError('Argument must be integer')
41
42 def __invert__(self):
43 print('Bitwise Ones Complement operator overloaded')
44 return Data(~self.id)
45
46 def __str__(self):
47 return f'Data[{self.id}]'
48
49d1 = Data(10)
50d2 = Data(7)
51
52print(f'd1&d2 = {d1&d2}')
53print(f'd1|d2 = {d1|d2}')
54print(f'd1^d2 = {d1^d2}')
55print(f'd1<<2 = {d1<<2}')
56print(f'd1>>2 = {d1>>2}')
57print(f'~d1 = {~d1}')
输出:
1Bitwise AND operator overloaded
2d1&d2 = Data[2]
3Bitwise OR operator overloaded
4d1|d2 = Data[15]
5Bitwise XOR operator overloaded
6d1^d2 = Data[13]
7Bitwise Left Shift operator overloaded
8d1<<2 = Data[40]
9Bitwise Right Shift operator overloaded
10d1>>2 = Data[2]
11Bitwise Ones Complement operator overloaded
12~d1 = Data[-11]
如果您不熟悉新的字符串格式,请阅读 f-strings in Python。
摘要
Python bitwise 操作员主要用于数学计算,我们也可以为我们的自定义类实现实现支持 bitwise 操作员的特定方法。