scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
404 stars 68 forks source link

Allow per-item operations on Arrays #272

Closed scott-griffiths closed 1 year ago

scott-griffiths commented 1 year ago

Mirroring what can be done (more efficiently) in numpy. For example:

>>> a = Array('float32', [10.0, 0.0, -3.0])
>>> a *= 2
>>> a
Array('float32', [20.0, 0.0, -6.0])

This should work for +, -, /, *. Note that + means something already when an iterable is being added but that shouldn't be confusing here. Oh, and we can also do >> and <<.

Not all formats and operation combinations will make sense. I think they're all OK for integers, and all except the shifts work for floats. I'd suggest not allowing any to work for hex, oct or bin formats.

How about logical operations? This would set the first bit of each item:

>>> a |= '0x80000000'
>>> a
Array('float32', [-20.0, 0.0, -6.0])

Should be easy to code if we're not too worried about efficiency for now.

scott-griffiths commented 1 year ago

Done in 4.1.