按位运算和使用

Bitwise operation and usage(按位运算和使用)

本文介绍了按位运算和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这段代码:

x = 1        # 0001
x << 2       # Shift left 2 bits: 0100
# Result: 4

x | 2        # Bitwise OR: 0011
# Result: 3

x & 1        # Bitwise AND: 0001
# Result: 1

我可以理解 Python(和其他语言)中的算术运算符,但我从来没有很好地理解按位"运算符.在上面的示例中(来自 Python 书籍),我了解左移,但不了解其他两个.

I can understand the arithmetic operators in Python (and other languages), but I never understood 'bitwise' operators quite well. In the above example (from a Python book), I understand the left-shift but not the other two.

另外,按位运算符实际用于什么?我会很感激一些例子.

Also, what are bitwise operators actually used for? I'd appreciate some examples.

推荐答案

位运算符是处理多位值的运算符,但从概念上讲是一次一位.

Bitwise operators are operators that work on multi-bit values, but conceptually one bit at a time.

  • AND 只有当 两个 的输入都为 1 时才为 1,否则为 0.
  • OR 如果其中一个或两个输入为 1,则为 1,否则为 0.
  • XOR 仅当 恰好一个 的输入为 1 时为 1,否则为 0.
  • NOT 仅当其输入为 0 时为 1,否则为 0.
  • AND is 1 only if both of its inputs are 1, otherwise it's 0.
  • OR is 1 if one or both of its inputs are 1, otherwise it's 0.
  • XOR is 1 only if exactly one of its inputs are 1, otherwise it's 0.
  • NOT is 1 only if its input is 0, otherwise it's 0.

这些通常可以最好地显示为真值表.输入可能性在顶部和左侧,结果位是输入交叉处显示的四个(在 NOT 的情况下为两个,因为它只有一个输入)值之一.

These can often be best shown as truth tables. Input possibilities are on the top and left, the resultant bit is one of the four (two in the case of NOT since it only has one input) values shown at the intersection of the inputs.

AND | 0 1     OR | 0 1     XOR | 0 1    NOT | 0 1
----+-----    ---+----     ----+----    ----+----
 0  | 0 0      0 | 0 1       0 | 0 1        | 1 0
 1  | 0 1      1 | 1 1       1 | 1 0

一个例子是,如果你只想要一个整数的低 4 位,你将它与 15(二进制 1111)相加,所以:

One example is if you only want the lower 4 bits of an integer, you AND it with 15 (binary 1111) so:

    201: 1100 1001
AND  15: 0000 1111
------------------
 IS   9  0000 1001

在这种情况下,15 中的零位有效地充当过滤器,迫使结果中的位也为零.

The zero bits in 15 in that case effectively act as a filter, forcing the bits in the result to be zero as well.

另外,>><< 通常作为位运算符包含在内,它们分别将一个值左右移位"一定位数,扔掉你要移动的一端的位,并在另一端输入零位.

In addition, >> and << are often included as bitwise operators, and they "shift" a value respectively right and left by a certain number of bits, throwing away bits that roll of the end you're shifting towards, and feeding in zero bits at the other end.

所以,例如:

1001 0101 >> 2 gives 0010 0101
1111 1111 << 4 gives 1111 0000

请注意,Python 中的左移是不寻常的,因为它没有使用固定宽度来丢弃位 - 虽然许多语言使用基于数据类型的固定宽度,但 Python 只是扩展宽度以适应额外的位.为了在 Python 中获得丢弃行为,您可以使用按位 进行左移,例如在 8 位值中左移四位:

Note that the left shift in Python is unusual in that it's not using a fixed width where bits are discarded - while many languages use a fixed width based on the data type, Python simply expands the width to cater for extra bits. In order to get the discarding behaviour in Python, you can follow a left shift with a bitwise and such as in an 8-bit value shifting left four bits:

bits8 = (bits8 << 4) & 255

考虑到这一点,位运算符的另一个示例是,如果您有两个 4 位值要打包成一个 8 位值,则可以使用所有三个运算符(left-shift, andor):

With that in mind, another example of bitwise operators is if you have two 4-bit values that you want to pack into an 8-bit one, you can use all three of your operators (left-shift, and and or):

packed_val = ((val1 & 15) << 4) | (val2 & 15)

  • &15 操作将确保两个值都只有低 4 位.
  • <代码><<4 是左移 4 位,将 val1 移动到 8 位值的前 4 位.
  • | 只是将这两者结合在一起.
    • The & 15 operation will make sure that both values only have the lower 4 bits.
    • The << 4 is a 4-bit shift left to move val1 into the top 4 bits of an 8-bit value.
    • The | simply combines these two together.
    • 如果 val1 为 7 且 val2 为 4:

      If val1 is 7 and val2 is 4:

                      val1            val2
                      ====            ====
       & 15 (and)   xxxx-0111       xxxx-0100  & 15
       << 4 (left)  0111-0000           |
                        |               |
                        +-------+-------+
                                |
      | (or)                0111-0100
      

      这篇关于按位运算和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:按位运算和使用

基础教程推荐