位运算和用法 [英] Bitwise operation and usage

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

问题描述

考虑这个code:

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.

此外,什么是位运算符实际使用的?我倒是AP preciate一些例子。

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.


  • 1只有当的两个的其输入为1,否则就是0。

  • 如果是1的一个或它的投入都的是1,否则是0。

  • XOR 1只有当正是其输入之一的是1,否则是0。

  • 不是 1只有当其输入为0,否则它是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.

这些通常可以最佳的示出为真值表。输入的可能性是在顶部和左侧,所得到的位值示出在输入的交叉点的四个(两个不因为它仅具有一个输入的情况下)中的一个。

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.

此外,&GT;&GT; &LT;&LT; 通常包括作为位运算符,它们转移分别向右和由一定数目的位的左,扔掉,你向移位端的辊位,而在另一端在零比特馈送值

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

考虑到这一点,位运算符的另一个例子是,如果您有想要打包成一个8位的一二4位的值,可以使用所有这三个操作员的(离开-Shift

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


  • &安培; 15 操作将确保这两个值只有低4位。

  • &LT;&LT; 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

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

      这篇关于位运算和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆