Bitwise operations in Python
Asked Answered
P

2

4

I'm looking for recommendations on how to do bitwise math in python.

The main problem I have is that python's bitwise operators have infinite precision, which means that -1 is really "111.......111". That's not what I want. I want to emulate real hardware which will have some fixed precision, say 32 bits.

Here are some gotchas:

1) -n should return a 32 bit 2's complement number ( this is easily achieved by taking the lower 32 bits of the infinite precision -n )

2) n >> 3, should be an arithmetic shift of a 32 bit number, which means if bit 31 is '1', then bits 31:28 should be '1' after the shift by 3.

Patras answered 13/5, 2011 at 15:38 Comment(6)
You could use numpy, it has built in int32 types. docs.scipy.org/doc/numpy-1.5.x/reference/arrays.scalars.htmlEponymy
GWW: I was just about to post this as an answer. Maybe you'd like to make it an answer yourself?Dunseath
@Sven Marnach: Sure I can post it thanks, I wasn't sure if he wanted to use external libraries or not.Eponymy
Thanks for the suggestion. Ideally I would want to have arbitrary sized integers though, not necessarily limited to 32, 64 etc. The hardware might very well have < 32 bit integers if 32 bit precision is not necessary.Patras
@DasBoot: When you say you want a 2's complement number that means you want to use negative integers as well correct?Eponymy
Yes. I want to use negative integers as well. Your numpy idea seems like it would work fine for standard sizes ( 8, 16, 32, ... ), but I would also like to specify some precision like 20.Patras
L
3

You could always add a & ((1<<32) - 1) mask to limit the number to 32-bits before performing any operation, e.g.

class Int32(int):
    def __neg__(self):
        return Int32(int.__neg__(self) & ((1 << 32) - 1))
    def __rshift__(self, other):
        if self & (-1 << 31):
             retval = int.__rshift__(int.__sub__(self, 1<<32), other)
             return Int32(retval & ((1 << 32) - 1))
        else:
             return Int32(int.__rshift__(self, other))
    ...

>>> -Int32(5)
4294967291
>>> (-Int32(5)) >> 1
4294967293
Libration answered 13/5, 2011 at 15:46 Comment(3)
This doesn't quite work for the >> operator though right? It will shift 0s instead of 1s in some cases.Patras
Ok. Nice. Anyway I can template this class? Have something like Int<N>?Patras
You can't really template classes in Python, but you can write the class so that it takes the desired word size at instantiation and behaves accordingly.Doublespace
E
6

You could use numpy, it has built in int32 types and much more.

Eponymy answered 13/5, 2011 at 15:44 Comment(0)
L
3

You could always add a & ((1<<32) - 1) mask to limit the number to 32-bits before performing any operation, e.g.

class Int32(int):
    def __neg__(self):
        return Int32(int.__neg__(self) & ((1 << 32) - 1))
    def __rshift__(self, other):
        if self & (-1 << 31):
             retval = int.__rshift__(int.__sub__(self, 1<<32), other)
             return Int32(retval & ((1 << 32) - 1))
        else:
             return Int32(int.__rshift__(self, other))
    ...

>>> -Int32(5)
4294967291
>>> (-Int32(5)) >> 1
4294967293
Libration answered 13/5, 2011 at 15:46 Comment(3)
This doesn't quite work for the >> operator though right? It will shift 0s instead of 1s in some cases.Patras
Ok. Nice. Anyway I can template this class? Have something like Int<N>?Patras
You can't really template classes in Python, but you can write the class so that it takes the desired word size at instantiation and behaves accordingly.Doublespace

© 2022 - 2024 — McMap. All rights reserved.