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

Benjamin Root ben.root at ou.edu
Tue Apr 28 11:59:15 EDT 2015


Yeah, I am not seeing any way around it at the moment. I guess I will have
to use the bitarray package for now. I was hoping for some fast per-element
processing, but at the moment, I guess I will have to sacrifice that just
to have something that worked correctly.

Ben Root

On Tue, Apr 28, 2015 at 10:19 AM, Jaime Fernández del Río <
jaime.frio at gmail.com> wrote:

> 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.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150428/250bb2ba/attachment.html>


More information about the NumPy-Discussion mailing list