-2

I am having trouble with the following function

def get_lexographically_next_bit_sequence(self, bits):
    """
    Bit hack from here:
    http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation

    Generator even does this in poker order rank 
    so no need to sort when done! Perfect.
    """
    t = (bits | (bits - 1)) + 1 
    next = t | ((((t & -t) // (bits & -bits)) >> 1) - 1)  
    yield next
    while True:
        t = (next | (next - 1)) + 1 
        next = t | ((((t & -t) // (next & -next)) >> 1) - 1)
        yield next

This function returns the error:

TypeError: unsupported operand type(s) for >>: 'float' and 'int'

Notes: This python library was only supported in 2.7 and I used 2to3 in order to use it. Other parts of the library work as desired, so I'm generally confident that 2to3 worked.

I am trying to run this in IPython 3.5 and I have heard that some errors like this can happen specifically within IPython, so I'm wondering if it's related to that.

Christian Gollhardt
  • 14,865
  • 16
  • 65
  • 99
Unknown Coder
  • 6,201
  • 19
  • 73
  • 120

1 Answers1

0

The issue arises from the fact that you are trying to preform a Binary Right Shift (>>) between two different data types (float and int). Casting the float down to an int with (int(((t & -t) // (next & -next)) >> 1) - 1) should work to the best of my knowledge.

Menachem Hornbacher
  • 1,759
  • 2
  • 23
  • 31