久久久久久久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 | 国产一级片在线播放 | 成人二区三区 | 91资源在线 | 91大神新作在线观看 | 人成久久 | 日韩中文一区二区三区 | 麻豆成人在线视频 | 日韩在线视频一区 | 国产精品久久久久久久久久免费看 | 久久国产精品首页 | jdav视频在线观看免费 | 成人欧美一区二区 | 男女羞羞视频免费看 | 欧美videosex性极品hd | 日韩乱码在线 | 自拍偷拍亚洲欧美 | 天天干夜夜操 | av日韩高清 | 精品成人在线视频 | 欧美日韩在线一区二区 | 久久久久久亚洲精品 | 亚洲一区二区精品视频 | 黄色网址在线免费观看 | 日日摸天天添天天添破 | 日韩精品在线看 | 精品久久久久久亚洲精品 | 最新国产视频 | 天天天天天操 | 欧美精品黄| 久久91| 久久av网| 精品国产欧美一区二区三区成人 | 亚洲一区二区在线视频 | 国产一区二区小视频 | 午夜在线小视频 | 国产成人精品免费视频 | 欧美一级二级三级视频 | 免费成人在线网站 | 国产成人久久精品一区二区三区 | 日韩欧美在线观看 |