dementrock
dementrock

Reputation: 937

How to get the logical right binary shift in python

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

(-1000) >>> 3 = 536870787

(-1000) >> 3 = -125

1000 >>> 3 = 125

1000 >> 3 = 125

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

Upvotes: 47

Views: 44543

Answers (11)

systemBuilder
systemBuilder

Reputation: 65

The top-voted answer (66 votes) produces WRONG results for val < 0 and n == 0! Here is a correct function - it produces correct values for 32-bit arithmetic and right shift of -1 by 0, 1, 2, 3.

def rshift(val, n):
    if (val >= 0): return val >> n
    elif (n == 0): return val
    else: return (val + 0x100000000) >> n

>>> rshift(-1, 0)
-1

Upvotes: -2

Scott Griffiths
Scott Griffiths

Reputation: 21925

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

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

Upvotes: 2

jathanism
jathanism

Reputation: 33706

Numpy provides the right_shift() function that does this:

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

Upvotes: 2

SomeOne
SomeOne

Reputation: 1

I think a logical right binary shift is not available in Python directly. Instead you can use Javascript in Python as in:

import js2py
rshift = js2py.eval_js('function $(a, b){ return a >>> b}')
print (rshift(244, 324)) #15

Upvotes: -4

Daniel
Daniel

Reputation: 318

You could also use floor division:

def rshift(val, n):
    if val > 0:
        return val >> n
    return val // -(2^n)

Upvotes: 0

Victor010
Victor010

Reputation: 1

A solution that works without a modulo:

>>> def rshift(val,n): return (val>>n) & (0x7fffffff>>(n-1))

This works since 7fffffff is a positive number and right shifting that will add zeros to the left.

Upvotes: 0

blackd0t
blackd0t

Reputation: 439

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

Upvotes: 1

Raphael
Raphael

Reputation: 21

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.

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

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308091

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.

def unsigned32(signed):
    return signed % 0x100000000

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

Upvotes: 3

NPE
NPE

Reputation: 500167

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:

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

Upvotes: 62

Femaref
Femaref

Reputation: 61427

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

Upvotes: 11

Related Questions