comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: counting bits
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: counting bits [message #34099 is a reply to message #34095] Tue, 18 February 2003 08:10 Go to previous messageGo to previous message
eddie haskell is currently offline  eddie haskell
Messages: 29
Registered: September 1998
Junior Member
Craig Markwardt wrote:

> Okay, here's a problem I've always solved by the brute force method:
>
> * what is the lowest / highest bit position set in an integer?
>
> For example, a the lowest bit position in the binary number 11010100
> is 2 (bit positions are labeled starting with 0 of course). The brute
> force method involves testing each bit in succession using something
> like (VALUE AND 2L^I) for each I, until a set bit is found.

I have not done any checking to see if this is any faster than a brute force
method but you could use something like this to get the lowest and highest set
bits:

IDL> n = 212 ;11010100
IDL> nbits = 8
IDL> print,(indgen(nbits))[(where((n and 2l^lindgen(nbits)) ne 0))[[0,nbits]]]

2 7

An issue with this is that N=0 will return [0,0] (the same as N=1), but
checking for a N of zero can easily be done beforehand.

Cheers,
eddie
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: running out of memory! can all memory be restored in idl?
Next Topic: Re: howto bind shortcut key to button

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 18:41:07 PDT 2025

Total time taken to generate the page: 0.32191 seconds