Extract Indices of Numpy Array Based on Given Bit Information

Chris Angelico rosuav at gmail.com
Sat Oct 18 02:24:39 EDT 2014


On Sat, Oct 18, 2014 at 4:58 PM, Artur Bercik <vbubbly21 at gmail.com> wrote:
> I want to get the index of my data where the following occurs:
>
> Bit No. 0–1
> Bit Combination: 00

So, what you want to do is look at each number in binary, and find the
ones that have two zeroes in the last two places?
1073741824: 1000000000000000000000000000000
1073741877: 1000000000000000000000000110101

The easiest way to do this is with simple bitwise operations:

>>> 1073741824 & 3
0
>>> 1073741877 & 3
1

3 is 0000000000000011 in binary, so a bitwise AND with that will tell
you about just the last two bits. The result will be an integer - 0,
1, 2, or 3, representing 00, 01, 10, or 11 for those last two bits. So
all you have to do is AND each number with 3, and when the result is
0, that's what you want.

Does that make sense?

ChrisA



More information about the Python-list mailing list