English 中文(简体)
哪怕是哪怕是哪里?
原标题:what is the diffenence beteen -8 >> 12 and (-8 >> 12) in python?

我试图了解借方转移操作>>关于负重整。

-2 >> 1 # -1
-3 >> 1 # -2
-5 >> 1 # -3
-7 >> 1 # -4

是否有人解释如何做到这一点? 我知道这涉及两个补充,但我可以不谈这一变化。

最佳回答

详细解释见

两份关于负面分类的辅助本:

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from "00000000" to "01111111" as the whole numbers from 0 to 127, and reserve "1xxxxxxx" for writing negative numbers. A negative number, -x, is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). So -1 is complement(1 - 1) = complement(0) = "11111111", and -10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110". This means that negative numbers go all the way down to -128 ("10000000").

Of course, Python doesn t use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011".

因此,关于转移经营人的解释:

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

In order to understand the above explanation you can check it out with something like this:

def twos_comp(val, nbits):
    """Compute the 2 s complement of int value val"""
    if val < 0:
        val = (1 << nbits) + val
    else:
        if (val & (1 << (nbits - 1))) != 0:
            # If sign bit is set.
            # compute negative value.
            val = val - (1 << nbits)
    return val

def foo(a,b):
    print("{0:b} >> {1:b} = {2:b} <==> {3:b} >> {4:b} = {5:b}".format(
        a,b,a>>b,
        twos_comp(a,8),b, twos_comp(a>>b,8)
    ))

foo(-2, 1)
foo(-3, 1)
foo(-5, 1)
foo(-7, 1)

哪些产出:

-10 >> 1 = -1 <==> 11111110 >> 1 = 11111111
-11 >> 1 = -10 <==> 11111101 >> 1 = 11111110
-101 >> 1 = -11 <==> 11111011 >> 1 = 11111101
-111 >> 1 = -100 <==> 11111001 >> 1 = 11111100

如你所看到的那样,这两处数字将予以扩大。

问题回答

暂无回答




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签