Extract Indices of Numpy Array Based on Given Bit Information

Chris Angelico rosuav at gmail.com
Sat Oct 18 08:50:47 CEST 2014

On Sat, Oct 18, 2014 at 5:42 PM, Artur Bercik <vbubbly21 at gmail.com> wrote:
> I got some sense, but could not imagine if required Bit No. 2–5, and  Bit
> Combination 0000.
> I hope example with the new case would make me more sense.

Just write the number in binary, with the bits you're interested in
set to 1, and everything else 0:

... 876543210
... 000111100

>>> 0b000111100
>>> 1073741877 & 0b000111100

So this number does _not_ have all zeroes there. If it did, the result
would be zero. To look for some other pattern, just do the same thing
- suppose we want to find numbers where it's 1001, we just look for
the result to be 0b000100100, or 36.


More information about the Python-list mailing list