Python 简明教程

Python - Bitwise Operators

Python Bitwise Operators

Python bitwise operators 通常用于对整数类型对象执行按位操作。但是,不是将 object 视为一个整体,而是将其视为一个位串。对 string 中的每个位执行不同的操作。

Python bitwise operators are normally used to perform bitwise operations on integer-type objects. However, instead of treating the object as a whole, it is treated as a string of bits. Different operations are done on each bit in the string.

Python 有六个按位运算符 - &、|、^、~、<< 和 >>。除了 ~ 之外,所有这些 operators 在本质上都是二进制的,因为它们对两个操作数进行操作。每个操作数都是二进制数字(位)1 或 0。

Python has six bitwise operators - &, |, ^, ~, << and >>. All these operators (except ~) are binary in nature, in the sense they operate on two operands. Each operand is a binary digit (bit) 1 or 0.

以下是 Python 中的按位运算符 -

The following are the bitwise operators in Python -

  1. Bitwise AND Operator

  2. Bitwise OR Operator

  3. Bitwise XOR Operator

  4. Bitwise NOT Operator

  5. Bitwise Left Shift Operator

  6. Biwtise Right Shift Operator

Python Bitwise AND Operator (&)

按位 AND 运算符有点类似于逻辑与运算符。仅当两个位操作数均为 1(即 True)时,它才返回 True。所有组合如下所示:

Bitwise AND operator is somewhat similar to logical and operator. It returns True only if both the bit operands are 1 (i.e. True). All the combinations are −

0 & 0 is 0
1 & 0 is 0
0 & 1 is 0
1 & 1 is 1

当您使用整数作为操作数时,这两个整数都会转换为等效的二进制,& 操作针对每个数字的对应位执行,从最低有效位开始朝最高有效位移动。

When you use integers as the operands, both are converted in equivalent binary, the & operation is done on corresponding bit from each number, starting from the least significant bit and going towards most significant bit.

Example of Bitwise AND Operator in Python

我们取两个整数 60 和 13,并将它们分别赋值给 variables a 和 b。

Let us take two integers 60 and 13, and assign them to variables a and b respectively.

a=60
b=13
print ("a:",a, "b:",b, "a&b:",a&b)

它将生成以下 output

It will produce the following output

a: 60 b: 13 a&b: 12

要了解 Python 如何执行操作,请获取每个变量的二进制等效值。

To understand how Python performs the operation, obtain the binary equivalent of each variable.

print ("a:", bin(a))
print ("b:", bin(b))

它将生成以下 output

It will produce the following output

a: 0b111100
b: 0b1101

为方便起见,为每个数字使用标准 8 位格式,这样“a”就是 00111100,“b”就是 00001101。让我们手动对这两个数字的每个对应位执行 and 操作。

For the sake of convenience, use the standard 8-bit format for each number, so that "a" is 00111100 and "b" is 00001101. Let us manually perform and operation on each corresponding bits of these two numbers.

0011 1100
&
0000 1101
-------------
0000 1100

将结果二进制值转换回整数。您将得到 12,这就是之前获得的结果。

Convert the resultant binary back to integer. You’ll get 12, which was the result obtained earlier.

>>> int('00001100',2)
12

Python Bitwise OR Operator (|)

“|”符号(称为 pipe )是按位 OR 运算符。如果任何一位操作数为 1,则结果为 1,否则为 0。

The "|" symbol (called pipe) is the bitwise OR operator. If any bit operand is 1, the result is 1 otherwise it is 0.

0 | 0 is 0
0 | 1 is 1
1 | 0 is 1
1 | 1 is 1

Example of Bitwise OR Operator in Python

取 a=60、b=13 的相同值。“|”操作的结果为 61。获得它们的二进制等效值。

Take the same values of a=60, b=13. The "|" operation results in 61. Obtain their binary equivalents.

a=60
b=13
print ("a:",a, "b:",b, "a|b:",a|b)
print ("a:", bin(a))
print ("b:", bin(b))

它将生成以下 output

It will produce the following output

a: 60 b: 13 a|b: 61
a: 0b111100
b: 0b1101

要手动执行“|”操作,请使用 8 位格式。

To perform the "|" operation manually, use the 8-bit format.

0011 1100
    |
0000 1101
-------------
0011 1101

将二进制数转换回整数以比较结果 −

Convert the binary number back to integer to tally the result −

>>> int('00111101',2)
61

Python Bitwise XOR Operator (^)

术语 XOR 代表或排除。这意味着如果只有其中一个位为 1,则对两个位执行或运算的结果将为 1。

The term XOR stands for exclusive OR. It means that the result of OR operation on two bits will be 1 if only one of the bits is 1.

0 ^ 0 is 0
0 ^ 1 is 1
1 ^ 0 is 1
1 ^ 1 is 0

Example of Bitwise XOR Operator in Python

让我们对 a=60 和 b=13 执行 XOR 运算。

Let us perform XOR operation on a=60 and b=13.

a=60
b=13
print ("a:",a, "b:",b, "a^b:",a^b)

它将生成以下 output

It will produce the following output

a: 60 b: 13 a^b: 49

现对位进行手动 XOR 运算。

We now perform the bitwise XOR manually.

0011 1100
    ^
0000 1101
-------------
0011 0001

int() 函数将 00110001 显示为 49。

The int() function shows 00110001 to be 49.

>>> int('00110001',2)
49

Python Bitwise NOT Operator (~)

这个运算符是逻辑 NOT 运算符的二进制等价项。它会翻转每个位,使 1 变成 0,0 变成 1,并返回原始数字的补数。Python 使用 2 的补码方法。对于正整数,可以通过简单地颠倒位来获得它。对于负数 -x,它使用 (x-1) 的位模式来编写,并且所有这些位都进行了补码(将 1 转换为 0 或 0 转换为 1)。因此:(对于 8 位表示)

This operator is the binary equivalent of logical NOT operator. It flips each bit so that 1 is replaced by 0, and 0 by 1, and returns the complement of the original number. Python uses 2’s complement method. For positive integers, it is obtained simply by reversing the bits. For negative number, -x, it is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). Hence: (for 8 bit representation)

-1 is complement(1 - 1) = complement(0) = "11111111"
-10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110".

Example of Bitwise NOT Operator in Python

对于 a=60,其补数为 −

For a=60, its complement is −

a=60
print ("a:",a, "~a:", ~a)

它将生成以下 output

It will produce the following output

a: 60 ~a: -61

Python Bitwise Left Shift Operator (<<)

左移位运算符将最显着的位向 “<<” 符号右侧的数字右移。因此,“x << 2” 将二进制表示中的两个位向右移动。

Left shift operator shifts most significant bits to right by the number on the right side of the "<<" symbol. Hence, "x << 2" causes two bits of the binary representation of to right.

Example of Bitwise Left Shift Operator in Python

让我们对 60 执行左移。

Let us perform left shift on 60.

a=60
print ("a:",a, "a<<2:", a<<2)

它将生成以下 output

It will produce the following output

a: 60 a<<2: 240

这是如何发生的?让我们使用 60 的二进制等价项,并向左移位 2。

How does this take place? Let us use the binary equivalent of 60, and perform the left shift by 2.

0011 1100
<<
    2
-------------
1111 0000

将二进制转换为整数。它为 240。

Convert the binary to integer. It is 240.

>>> int('11110000',2)
240

Python Bitwise Right Shift Operator (>>)

右移位运算符将最低有效位向 “>>” 符号右侧的数字左移。因此,“x >> 2” 将二进制表示中的两个位向左移动。

Right shift operator shifts least significant bits to left by the number on the right side of the ">>" symbol. Hence, "x >> 2" causes two bits of the binary representation of to left.

Example of Bitwise Right Shift Operator in Python

让我们对 60 执行右移。

Let us perform right shift on 60.

a=60
print ("a:",a, "a>>2:", a>>2)

它将生成以下 output

It will produce the following output

a: 60 a>>2: 15

对 60 执行的手动右移位运算如下 −

Manual right shift operation on 60 is shown below −

   0011 1100
   >>
   2
   -------------
   0000 1111

使用 int() 函数将上述二进制数转换为整数。它是 15。

Use int() function to covert the above binary number to integer. It is 15.

>>> int('00001111',2)
15