How to get the logical right binary shift in python

时间:2022-04-04 12:08:40

As revealed by the title. In JavaScript there is a specific operator '>>>'. For example, in JavaScript we will have the following result:

正如标题所揭示的那样。在JavaScript中有一个特定的运算符'>>>'。例如,在JavaScript中我们将得到以下结果:

(-1000) >>> 3 = 536870787

(-1000)>>> 3 = 536870787

(-1000) >> 3 = -125

(-1000)>> 3 = -125

1000 >>> 3 = 125

1000 >>> 3 = 125

1000 >> 3 = 125

1000 >> 3 = 125

So is there a certain method or operator representing this '>>>'?

那么是否有某种方法或运算符代表这个'>>>'?

7 个解决方案

#1


31  

There isn't a built-in operator for this, but you can easily simulate the >>> yourself:

没有内置的操作符,但您可以自己轻松地模拟>>>:

>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n
... 
>>> rshift(-1000, 3)
536870787
>>> rshift(1000, 3)
125

The following alternative implementation removes the need for the if:

以下替代实现消除了对if的需要:

>>> def rshift(val, n): return (val % 0x100000000) >> n

#2


7  

No, there isn't. The right shift in python is arithmetical.

不,没有。 python的正确转变是算术的。

#3


3  

Numpy provides the right_shift() function that does this:

Numpy提供了right_shift()函数来执行此操作:

>>> import numpy
>>> numpy.right_shift(1000, 3)
125

#4


2  

You can do a bitwise shift padding with zeros with the bitstring module using the >>= operator:

您可以使用>> =运算符使用bitstring模块使用零进行逐位移位填充:

>>> a = BitArray(int=-1000, length=32)
>>> a.int
-1000
>>> a >>= 3
>>> a.int
536870787

#5


1  

Here's a spinoff of aix's answer. The normal right-shift operator will work if you feed it a positive value, so you're really looking for a conversion from signed to unsigned.

这是aix答案的副产品。如果你给它一个正值,那么正常的右移运算符就可以工作,所以你真的在寻找从有符号到无符号的转换。

def unsigned32(signed):
    return signed % 0x100000000

>>> unsigned32(-1000) >> 3
536870787L

#6


1  

Trying to flip the sign bit of a negative number by masking it with 0x100000000 is fundamentally misconceived as it makes hard assumptions about the word length. In my time as a programmer I have worked with 24-, 48-, 16-, 18-, 32-, 36- and 64-bit numbers. I have also heard of machines that work on odd-lengths, such as 37 and others that use ones-complement, and not twos-complement, arithmetic. Any assumptions you make about the internal representation of numbers, beyond that they are binary, is dangerous.

试图通过用0x100000000屏蔽它来翻转负数的符号位从根本上是错误的,因为它对字长做出了很难的假设。在我作为程序员的时候,我使用过24位,48位,16位,18位,32位,36位和64位数字。我也听说过使用奇数长度的机器,例如37和其他使用补码的机器,而不是二进制补码算术。您对数字的内部表示做出的任何假设,除了它们是二进制之外,都是危险的。

Even the binary assumption is not absolutely safe, but I think we'll allow that. :)

即使是二元假设也不是绝对安全的,但我认为我们会允许这样做。 :)

#7


1  

You need to remember that if the number is negative, the top bit is set and with each shift right you need to make the top bit set as well.

您需要记住,如果数字为负数,则设置最高位,并且每个右移都需要设置最高位。

Here is my implementation:

这是我的实施:

def rshift(val, n):
    s = val & 0x80000000
    for i in range(0,n):
        val >>= 1
        val |= s
    return val

#1


31  

There isn't a built-in operator for this, but you can easily simulate the >>> yourself:

没有内置的操作符,但您可以自己轻松地模拟>>>:

>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n
... 
>>> rshift(-1000, 3)
536870787
>>> rshift(1000, 3)
125

The following alternative implementation removes the need for the if:

以下替代实现消除了对if的需要:

>>> def rshift(val, n): return (val % 0x100000000) >> n

#2


7  

No, there isn't. The right shift in python is arithmetical.

不,没有。 python的正确转变是算术的。

#3


3  

Numpy provides the right_shift() function that does this:

Numpy提供了right_shift()函数来执行此操作:

>>> import numpy
>>> numpy.right_shift(1000, 3)
125

#4


2  

You can do a bitwise shift padding with zeros with the bitstring module using the >>= operator:

您可以使用>> =运算符使用bitstring模块使用零进行逐位移位填充:

>>> a = BitArray(int=-1000, length=32)
>>> a.int
-1000
>>> a >>= 3
>>> a.int
536870787

#5


1  

Here's a spinoff of aix's answer. The normal right-shift operator will work if you feed it a positive value, so you're really looking for a conversion from signed to unsigned.

这是aix答案的副产品。如果你给它一个正值,那么正常的右移运算符就可以工作,所以你真的在寻找从有符号到无符号的转换。

def unsigned32(signed):
    return signed % 0x100000000

>>> unsigned32(-1000) >> 3
536870787L

#6


1  

Trying to flip the sign bit of a negative number by masking it with 0x100000000 is fundamentally misconceived as it makes hard assumptions about the word length. In my time as a programmer I have worked with 24-, 48-, 16-, 18-, 32-, 36- and 64-bit numbers. I have also heard of machines that work on odd-lengths, such as 37 and others that use ones-complement, and not twos-complement, arithmetic. Any assumptions you make about the internal representation of numbers, beyond that they are binary, is dangerous.

试图通过用0x100000000屏蔽它来翻转负数的符号位从根本上是错误的,因为它对字长做出了很难的假设。在我作为程序员的时候,我使用过24位,48位,16位,18位,32位,36位和64位数字。我也听说过使用奇数长度的机器,例如37和其他使用补码的机器,而不是二进制补码算术。您对数字的内部表示做出的任何假设,除了它们是二进制之外,都是危险的。

Even the binary assumption is not absolutely safe, but I think we'll allow that. :)

即使是二元假设也不是绝对安全的,但我认为我们会允许这样做。 :)

#7


1  

You need to remember that if the number is negative, the top bit is set and with each shift right you need to make the top bit set as well.

您需要记住,如果数字为负数,则设置最高位,并且每个右移都需要设置最高位。

Here is my implementation:

这是我的实施:

def rshift(val, n):
    s = val & 0x80000000
    for i in range(0,n):
        val >>= 1
        val |= s
    return val