美文网首页
Shift operation

Shift operation

作者: SharlotteZZZ | 来源:发表于2018-08-18 06:10 被阅读0次

    x << y

    Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

    x >> y

    Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

    x & y

    Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.

    # 1 & odd/even
    In[1]:  1 & 1
    Out[1]: 1
    In[2]:  1 & 2
    Out[2]: 0
    In[3]:  1 & 3
    Out[3]: 1
    In[4]:  1 & 4
    Out[4]: 0
    In[5]:  1 & 5
    Out[5]: 1
    In[6]:  1 & 6
    Out[6]: 0
    

    x | y

    Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.

    ~ x

    Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.

    x ^ y

    Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.

    相关文章

      网友评论

          本文标题:Shift operation

          本文链接:https://www.haomeiwen.com/subject/ednabftx.html