Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79256 > unrolled thread

Re: Extract Indices of Numpy Array Based on Given Bit Information

Started byChris Angelico <rosuav@gmail.com>
First post2014-10-18 17:24 +1100
Last post2014-10-18 17:24 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Extract Indices of Numpy Array Based on Given Bit Information Chris Angelico <rosuav@gmail.com> - 2014-10-18 17:24 +1100

#79256 — Re: Extract Indices of Numpy Array Based on Given Bit Information

FromChris Angelico <rosuav@gmail.com>
Date2014-10-18 17:24 +1100
SubjectRe: Extract Indices of Numpy Array Based on Given Bit Information
Message-ID<mailman.14956.1413613488.18130.python-list@python.org>
On Sat, Oct 18, 2014 at 4:58 PM, Artur Bercik <vbubbly21@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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web