Python 位运算符 in Chinese can be paraphrased as “Python 位运算符”.

Python中的位运算符是用于对整数执行位运算的。这些整数被转换为二进制格式,然后一位一位执行操作,因此称为位运算符。Python的位运算符仅适用于整数,最终的输出以十进制格式返回。Python的位运算符也被称为二进制运算符。

Python位运算符

Python中有6个位运算符。下表提供了有关它们的简要详情。

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

我们逐一研究这些运算符,了解它们的工作原理。

1. 位与运算符

Python 的位与运算符在两个位都为 1 时返回 1,否则返回 0。

>>> 10&7
2
>>> 
Python Bitwise And Operator

2. 按位或运算符

Python的位或运算符只要有任意一位是1,就返回1。如果两位都是0,那么返回0。

>>> 10|7
15
>>> 
Python Bitwise Or Operator

3. 位异或运算符

Python 的位异或运算符返回 1,当一位为 0 而另一位为 1 时。如果两位都是 0 或 1,则返回 0。

>>> 10^7
13
>>> 
Python Bitwise Xor Operator

4. 位运算取反操作符

在Python中,一个数字’A’的取反等于-(A+1)。

>>> ~10
-11
>>> ~-10
9
>>> 
Python Bitwise Ones Complement Operator

5. 按位左移操作符

Python的按位左移运算符将左操作数的位向左移动给定次数,位数由右操作数决定。简单来说,二进制数字的末尾附加了0。

>>> 10 << 2
40
>>> 
Python Bitwise Left Shift Operator

6. 位右移运算符

Python的右位移运算符与左位移运算符正好相反。然后,左侧操作数的比特位将向右侧移动给定次数。简单来说,右侧的比特位会被移除。

>>> 10 >> 2
2
>>>  
Python Bitwise Right Shift Operator

Python位运算符重载

Python 支持运算符重载。我们可以实现各种方法来支持自定义对象的位运算符。

Bitwise Operator Method to Implement
& __and__(self, other)
^ __xor__(self, other)
~ __invert__(self)
<< __lshift__(self, other)
>> __rshift__(self, other)

这里是我们自定义对象进行位运算符重载的一个例子。

class Data:
    id = 0

    def __init__(self, i):
        self.id = i

    def __and__(self, other):
        print('Bitwise AND operator overloaded')
        if isinstance(other, Data):
            return Data(self.id & other.id)
        else:
            raise ValueError('Argument must be object of Data')

    def __or__(self, other):
        print('Bitwise OR operator overloaded')
        if isinstance(other, Data):
            return Data(self.id | other.id)
        else:
            raise ValueError('Argument must be object of Data')

    def __xor__(self, other):
        print('Bitwise XOR operator overloaded')
        if isinstance(other, Data):
            return Data(self.id ^ other.id)
        else:
            raise ValueError('Argument must be object of Data')

    def __lshift__(self, other):
        print('Bitwise Left Shift operator overloaded')
        if isinstance(other, int):
            return Data(self.id << other)
        else:
            raise ValueError('Argument must be integer')

    def __rshift__(self, other):
        print('Bitwise Right Shift operator overloaded')
        if isinstance(other, int):
            return Data(self.id >> other)
        else:
            raise ValueError('Argument must be integer')

    def __invert__(self):
        print('Bitwise Ones Complement operator overloaded')
        return Data(~self.id)

    def __str__(self):
        return f'Data[{self.id}]'


d1 = Data(10)
d2 = Data(7)

print(f'd1&d2 = {d1&d2}')
print(f'd1|d2 = {d1|d2}')
print(f'd1^d2 = {d1^d2}')
print(f'd1<<2 = {d1<<2}')
print(f'd1>>2 = {d1>>2}')
print(f'~d1 = {~d1}')

输出:

Bitwise AND operator overloaded
d1&d2 = Data[2]
Bitwise OR operator overloaded
d1|d2 = Data[15]
Bitwise XOR operator overloaded
d1^d2 = Data[13]
Bitwise Left Shift operator overloaded
d1<<2 = Data[40]
Bitwise Right Shift operator overloaded
d1>>2 = Data[2]
Bitwise Ones Complement operator overloaded
~d1 = Data[-11]

如果你对新的字符串格式化方式不熟悉,请阅读Python中的f-strings。

总结

请用中文进行准确的总结。

Python的位运算符主要用于数学计算。我们还可以针对我们的自定义类实现特定的方法来支持位运算符。

bannerAds