scott-griffiths / bitstring

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

More operations on Arrays #288

Closed scott-griffiths closed 11 months ago

scott-griffiths commented 11 months ago

One I missed was unary minus (__neg__), which makes sense for Arrays of int or float-like types. Also should support unary plus, and abs.

>>> a = Array('float16', [-45.0, 0, 17])
>>> abs(a) 
Array('float16', [45.0, 0.0, 17.0]

There are also the rich comparison methods, such as __lt__, __eq__ etc. that typically return a bool. I don't think it's useful to return a bool, but we could return a new Array of bools:

>>> a = Array('uint8', [5, 10, 20, 100])
>>> a < 15
Array('bool', [True, True, False, False])
>>> a == 20
Array('bool', [False, False, True, False])