0

I have a trouble to make a function that may shift a bit more than 64 sites.

I'm making an mapping and inverse mapping function on an array table. I fill the table with numbers having only two 1 bits in ascending order. The mapping function map(number) returns the positions of two bits in table[number]. Inverse mapping function imap(k,l) works as it make a number with two bits at k and l site. And it returns the index where the number is in the table.

For example, I generate table as

idx number(binary)
0       11
1       101
2       110
3       1001
...      ...

If I use map(3), it will return (3,0) If I use imap(3,0), it will return 3

Here is the problem: If I use imap(63,0), it fails to find the corresponding number in table even though table contains the number. My code looks like

def imap(k,l):
        code=(1<<k)^(1<<l)
        from bisect import bisect_left
        idx=bisect_left(table,code) 
        if idx!=len(table) and table[idx]==code:
                return idx
        raise valueError

And I've found that code=(1<<k)^(1<<l) part throws -111111111111111111111111111111111111111111111111111111111111111 in my code. I test (1<<63)^(1<<0) in python3 idle, it return

>>> format((1<<63)^(1<<0),'b')
'1000000000000000000000000000000000000000000000000000000000000001'

Why they show different result?

Juhee Lee
  • 35
  • 6

0 Answers0