Experimental Physics and Industrial Control System
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
- Replies:
- RE: Select MS Bit from an integer Mooney, Tim M.
- Re: Select MS Bit from an integer Till Straumann
- References:
- Select MS Bit from an integer Dudley, David
- Re: Select MS Bit from an integer Till Straumann
- Navigate by Date:
- Prev:
Re: Select MS Bit from an integer Till Straumann
- Next:
RE: Select MS Bit from an integer Mooney, Tim M.
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
<2015>
2016
2017
2018
2019
2020
2021
2022
2023
2024
- Navigate by Thread:
- Prev:
Re: Select MS Bit from an integer Till Straumann
- Next:
RE: Select MS Bit from an integer Mooney, Tim M.
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
<2015>
2016
2017
2018
2019
2020
2021
2022
2023
2024