EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  <20152016  2017  2018  2019  2020  2021  2022  2023  2024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  <20152016  2017  2018  2019  2020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: RE: Select MS Bit from an integer
From: "Mooney, Tim M." <[email protected]>
To: "Johnson, Andrew N." <[email protected]>, "[email protected]" <[email protected]>
Date: Fri, 4 Sep 2015 17:46:12 +0000
If it's ok to use records not in base, you could do it like this with a scalcout:

CALC="A==0?0:(B:=31;A&(1<<B)?B:(L:=UNTIL(B:=B-1;A&(1<<B)));B)"


Write the number whose MSb you want to the A field.  The result will be the bit position (0..31) of the MS bit.
The UNTIL function evaluates it expression repeatedly until the result is nonzero (or 1000 iterations have occurred).

Tim Mooney ([email protected]) (630)252-5417
Beamline Controls Group (www.aps.anl.gov)
Advanced Photon Source, Argonne National Lab


________________________________________
From: [email protected] [[email protected]] on behalf of Andrew Johnson [[email protected]]
Sent: Friday, September 04, 2015 12:10 PM
To: [email protected]
Subject: Re: Select MS Bit from an integer

On 09/04/2015 11:12 AM, Till Straumann wrote:
> You could do it with a bunch of calc (C1..C5, V) records
>
>  C1 computes  (IN & 0xffff0000)
>  C2 computes  (c1 ? c1 : IN ) & 0xff00ff00
>  C3 computes  (c2 ? c2 : c1 ? c1 : IN) & 0xf0f0f0f0
>  C4 computes  (c3 ? c3 : c2 ? c2 : c1 ? c1 : IN) & 0xcccccccc
>  C5 computes  (c4 ? c4 : c3 ? c3 : c2 ? c2 : c1 ? c1 : IN) & 0xaaaaaaaa
>
> V computes  (C1 ? 16 : 0) + (C2 ? 8 : 0) + (C3 ? 4 : 0) + (C4 ? 2 : 0) +
> (C5 ? 1 : 0)

If I've understood this code properly V will be the bit number of the
highest set bit, from 0 through 31. It doesn't distinguish between no
bits set and only the LSB set though, you'd have to detect that separately.

If you have the synapps calc module installed a single transform record
could do all these calculations, transform handles up to 16 expressions.

You can find lots of bit-manipulation hacks at
    https://graphics.stanford.edu/~seander/bithacks.html

- Andrew

> On 09/04/2015 07:59 AM, Dudley, David wrote:
>>
>> Quick question that I didn’t see an immediate way to perform
>>
>> How do I select the most-significant set bit in an integer?  I was
>> thinking about using a SEL, but that seems like a lot of work just to
>> do a simple thing.  Other possibility would be a SUB, but I’d like to
>> do it with base record types if I can.
>>
>> Something simple please…

--
Light thinks it travels faster than anything but it is wrong.
No matter how fast light travels, it finds the darkness has
always got there first, and is waiting for it.
    -- Terry Pratchett, Reaper Man


References:
Select MS Bit from an integer Dudley, David
Re: Select MS Bit from an integer Till Straumann
Re: Select MS Bit from an integer Andrew Johnson

Navigate by Date:
Prev: Re: Select MS Bit from an integer Andrew Johnson
Next: Spellman's Bertan 225 HV power supply James Rezende Piton
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  <20152016  2017  2018  2019  2020  2021  2022  2023  2024 
Navigate by Thread:
Prev: Re: Select MS Bit from an integer Andrew Johnson
Next: Re: Select MS Bit from an integer Till Straumann
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  <20152016  2017  2018  2019  2020  2021  2022  2023  2024 
ANJ, 16 Dec 2015 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·