3

I have a bit array named c0 containing 28 bits

bitarray('1111011111111111111111110001')

how can I left shift this bit array for a number of times, meaning one left shift, two left shift, etc.? One left shift is fine for now!

NOTE: This pertains to the bitarray package.

7
  • 1
    There's no bitarray in standard Python. Are you using the PyPI package of the same name? Commented Dec 18, 2013 at 18:38
  • 1
    Is it pypi.python.org/pypi/bitarray ? Commented Dec 18, 2013 at 18:38
  • << and >> are the shift operators (the bitarray class should override the __ilshift__ and __irshift__ methods Commented Dec 18, 2013 at 18:39
  • 1
    @SimeonVisser: We can assume so Commented Dec 18, 2013 at 18:51
  • @SimeonVisser :Yes, It is bitarray. Could You give me a code that just implements one left shift In an example? Commented Dec 18, 2013 at 18:53

2 Answers 2

7

You could use slicing:

def leftshift(ba, count):
    return ba[count:] + (bitarray('0') * count)

def rightshift(ba, count):
    return (bitarray('0') * count) + ba[:-count]

These maintain the bit-width of the input, dropping bits on one end and padding with 0 on the other.

You can create your own subclass of the bitarray type:

class mybitarray(bitarray):
    def __lshift__(self, count):
        return self[count:] + type(self)('0') * count
    def __rshift__(self, count):
        return type(self)('0') * count + self[:-count]
    def __repr__(self):
        return "{}('{}')".format(type(self).__name__, self.to01())

Demo:

>>> c0 = mybitarray(c0)
>>> c0
mybitarray('1111011111111111111111110001')
>>> c0 << 4
mybitarray('0111111111111111111100010000')
>>> c0 >> 4
mybitarray('0000111101111111111111111111')

You can also poke the author to support these operations natively.

Sign up to request clarification or add additional context in comments.

Comments

1

<< and >> are the shift operators (the bitarray class should override the __ilshift__ and __irshift__ methods.

6 Comments

This is a third party library, written in C.
@MartijnPieters Third party libraries written in C can very well overload operators. The C syntax for overloading is different, but the resulting objects should have a __rshift__ attribute for instance, so this technicality is only important for the writer of the extension module. The only missing piece is an assertion (or better, reference) that it does.
there are a few bitarray classes for python floating around. Some are pure python and implement the shift, others do not. Need to know more.
@delnan: Correction, it does not.
@MartijnPieters Then complain about that, not about the fact that it's third party and written in C ;-) All assuming OP does use the package we're assuming and not the one Mike McMahon assumes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.