久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

按位運算和使用

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
      

      這篇關于按位運算和使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产高清在线观看 | 一区二区免费在线 | 久久在看 | 黄色毛片免费视频 | 91色在线视频 | 看片wwwwwwwwwww| 成人在线视频观看 | 亚洲综合区 | 91国产视频在线观看 | 久久精品视频一区二区三区 | 久久久久国产一区二区三区四区 | 日本91av视频 | 久久九九99| 国产欧美综合在线 | 日本精品在线一区 | 久久久精品网站 | 亚洲一区二区三区免费在线观看 | 九九天堂网 | 成年人在线视频 | 久久精品国产一区 | 中文字幕亚洲在线 | 欧美精品久久久 | 国产精品久久久久久久久久三级 | 国产成人精品午夜视频免费 | 91在线视频精品 | 欧美日韩免费一区二区三区 | 日韩欧美三区 | 人人干人人干人人干 | 日韩国产中文字幕 | 久久99精品久久久久久狂牛 | 日日人人 | 国产欧美一区二区三区在线看 | 欧美区日韩区 | 成人在线免费观看av | 久久r免费视频 | 紧缚调教一区二区三区视频 | 在线观看国产视频 | 免费在线一区二区 | 日韩精品一区二区久久 | 99久久精品国产毛片 | 午夜视频在线免费观看 |