[Numpy-discussion] Extract Indices of Numpy Array Based on Given Bit Information

Julian Taylor jtaylor.debian at googlemail.com
Sat Oct 18 08:28:30 EDT 2014


On 18.10.2014 14:14, Artur Bercik wrote:
> 
> 
> On Sat, Oct 18, 2014 at 9:00 PM, Artur Bercik <vbubbly21 at gmail.com
> <mailto:vbubbly21 at gmail.com>> wrote:
> 
> 
> 
>     On Sat, Oct 18, 2014 at 8:28 PM, Julian Taylor
>     <jtaylor.debian at googlemail.com
>     <mailto:jtaylor.debian at googlemail.com>> wrote:
> 
>         On 18.10.2014 07:58, Artur Bercik wrote:
>         > Dear Python and Numpy Users:
>         >
>         > My data are in the form of '32-bit unsigned integer' as follows:
>         >
>         > myData = np.array([1073741824, 1073741877, 1073742657, 1073742709,
>         > 1073742723, 1073755137, 1073755189,1073755969],dtype=np.int32)
>         >
>         > I want to get the index of my data where the following occurs:
>         >
>         > Bit No. 0–1
>         > Bit Combination: 00
>         >
>         > How can I do it? I heard this type of problem first time, please help me.
>         >
>         > Artur
>         >
> 
>         not sure I understand the problem, maybe this?
> 
>         np.where((myData & 0x3) == 0)
> 
> 
>     yes, it works greatly for the following case:
> 
>     myData = np.array([1073741824, 1073741877, 1073742657, 1073742709,
>     1073742723, 1073755137, 1073755189,1073755969],dtype=np.uint32)
>     Bit No. 0–1
>     Bit Combination: 00
> 
>     Can you make such automation for the following case as well?
> 
>     Bit No. 2–5
>     Bit Combination: 1101
> 

sure, you can do any of these with the right masks:
np.where((myData & 0x3c) == 0x34)

you can use bin(number) to check if your numbers are correct.


> 
> Also wondering why np.where((myData & 0x3) == 0) instead of
> just np.where((myData & 3) == 0) 
> 

its the same, 0x means the number is in hexadecimal representation, for
3 they happen to be equal (as 3 < 10)
It is often easier to work in the hexadecimal representation when
dealing with binary data as its base is a power of two. So two digits in
hexadecimal represent one byte.
In the case above: 0x3c
c is 12 -> 1100
3 is 3  -> 11
together you get 111100, mask for bits 2-5



More information about the NumPy-Discussion mailing list