[Numpy-discussion] how to set a fixed sized dtype suitable for bitwise operations

Jaime Fernández del Río jaime.frio at gmail.com
Tue Apr 28 10:19:20 EDT 2015


On Tue, Apr 28, 2015 at 7:00 AM, Benjamin Root <ben.root at ou.edu> wrote:

> I have a need to have a numpy array of 17 byte (more specifically, at
> least 147 bits) values that I would be doing some bit twiddling on. I have
> found that doing a dtype of "i17" yields a dtype of int32, which is
> completely not what I intended. Doing 'u17' gets an "data type not
> understood". I have tried 'a17', but then bitwise_or() and left_shift() do
> not work (returns "NotImplemented").
>

> How should I be going about this?
>

The correct type to use would be a void dtype:

>>> dt = np.dtype('V17')
>>> dt.itemsize
17

Unfortunately, it does not support bitwise operations either, which seems
like an oddity to me:

>>> a = np.empty(2, dt)
>>> a[0] = 'abcdef'
>>> a[1] = bytearray([64, 56, 78])
>>> a[0] | a[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'numpy.void' and 'numpy.void'

Any fundamental reason for this?

Jaime

-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150428/216653cf/attachment.html>


More information about the NumPy-Discussion mailing list