python 按位运算符_Python按位运算符

python 按位运算符

Python bitwise operators are used to perform bitwise calculations on integers. The integers are converted into binary format and then operations are performed bit by bit, hence the name bitwise operators.

Python按位运算符用于对整数执行按位计算。 整数转换为二进制格式,然后逐位执行运算,因此命名为按位运算符。

Python bitwise operators work on integers only and the final output is returned in the decimal format. Python bitwise operators are also called binary operators.

Python按位运算符仅对整数起作用,并且最终输出以十进制格式返回。 Python按位运算符也称为二进制运算符。

Python按位运算符 (Python Bitwise Operators)

There are 6 bitwise operators in Python. The below table provides short details about them.

Python中有6个按位运算符。 下表提供了有关它们的简短详细信息。

Bitwise OperatorDescriptionSimple Example
&Bitwise AND Operator10 & 7 = 2
|Bitwise OR Operator10 | 7 = 15
^Bitwise XOR Operator10 ^ 7 = 13
~Bitwise Ones’ Compliment Operator~10 = -11
<<Bitwise Left Shift operator10<<2 = 40
>>Bitwise Right Shift Operator10>>1 = 5
按位运算符 描述 简单的例子
按位与运算符 10&7 = 2
| 按位或运算符 10 | 7 = 15
^ 按位XOR运算符 10 ^ 7 = 13
按位补码运算符 〜10 = -11
<< 按位左移运算符 10 << 2 = 40
>> 按位右移运算符 10 >> 1 = 5

Let’s look into these operators one by one and understand how they work.

让我们逐一研究这些运算符,并了解它们的工作方式。

1.按位与运算符 (1. Bitwise AND Operator)

Python bitwise and operator returns 1 if both the bits are 1, otherwise 0.

如果两个位均为1,Python按位运算符将返回1,否则返回0。

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

Python Bitwise And Operator

Python按位与运算符

2.按位或运算符 (2. Bitwise OR Operator)

Python bitwise or operator returns 1 if any of the bits is 1. If both the bits are 0, then it returns 0.

如果任何位为1,Python按位或运算符将返回1。如果两个位均为0,则它​​将返回0。

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

Python Bitwise Or Operator

Python按位或运算符

3.按位XOR运算符 (3. Bitwise XOR Operator)

Python bitwise XOR operator returns 1 if one of the bits is 0 and the other bit is 1. If both the bits are 0 or 1, then it returns 0.

如果位之一为0,另一位为1,则Python按位XOR运算符将返回1。如果两个位均为0或1,则它将返回0。

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

Python Bitwise XOR Operator

Python按位XOR运算符

4.按位补码运算符 (4. Bitwise Ones’ Complement Operator)

Python Ones’ complement of a number ‘A’ is equal to -(A+1).

Python Ones的数字'A'的补码等于-(A + 1)。

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

Python Bitwise Ones Complement Operator

Python逐位补码运算符

5.按位左移运算符 (5. Bitwise Left Shift Operator)

Python bitwise left shift operator shifts the left operand bits towards the left side for the given number of times in the right operand. In simple terms, the binary number is appended with 0s at the end.

Python按位左移运算符将右操作数中的给定次数向左移动左操作数位。 简而言之,二进制数末尾带有0。

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

Python Bitwise Left Shift Operator

Python按位左移运算符

6.按位右移运算符 (6. Bitwise Right Shift Operator)

Python right shift operator is exactly the opposite of the left shift operator. Then left side operand bits are moved towards the right side for the given number of times. In simple terms, the right side bits are removed.

Python的右移运算符与左移运算符完全相反。 然后将左侧操作数位向右侧移动给定次数。 简单来说,右侧位已删除。

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

Python Bitwise Right Shift Operator

Python按位右移运算符

Python按位运算符重载 (Python Bitwise Operator Overloading)

Python supports operator overloading. There are various methods that we can implement to support bitwise operators for our custom objects.

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

Bitwise OperatorMethod to Implement
&__and__(self, other)
|__or__(self, other)
^__xor__(self, other)
~__invert__(self)
<<__lshift__(self, other)
>>__rshift__(self, other)
按位运算符 实施方法
__和__(自己,其他)
| __或__(自己,其他)
^ __xor __(自己,其他)
__invert __(个体经营)
<< __lshift __(自己,其他)
>> __rshift __(自己,其他)

Here is an example of a bitwise operator overloading for our custom object.

这是我们的自定义对象按位运算符重载的示例。

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}')

Output:

输出:

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]

摘要 (Summary)

Python bitwise operators are mostly used in mathematical calculations. We can implement specific methods to support bitwise operators for our custom class implementations too.

Python按位运算符通常用于数学计算。 我们也可以实现特定的方法来为我们的自定义类实现支持按位运算符。

翻译自: https://www.journaldev.com/26737/python-bitwise-operators

python 按位运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值