問題描述
考慮這段代碼:
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
, and
和 or
):
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 moveval1
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
這篇關于按位運算和使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!