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 2025 | 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 2025 |
<== Date ==> | <== Thread ==> |
---|
Subject: | Re: Select MS Bit from an integer |
From: | Till Straumann <[email protected]> |
To: | Andrew Johnson <[email protected]>, [email protected] |
Date: | Fri, 4 Sep 2015 14:01:29 -0700 |
On 09/04/2015 10:10 AM, Andrew Johnson wrote:
Yes - depends on what the result should be. I just intended to present the idea.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.
The last line could e.g., be IN ? (C1 ? 16 : 0) + (C2 ? 8 : 0) + (C3 ? 4 : 0) + (C4 ? 2 : 0) + (C5 ? 1 : 0) : -1 which would yield -1 if no bits are set. The expressions could be simplified at the expense of more records C1 = (IN & 0xffff0000) C1C = C1 ? C1 : IN; C2 = C1C & 0xff00ff00 C2C = C2 ? C2 : C1C C3 = C2C & 0xf0f0f0f0 C3C = C3 ? C3 : C2C C4 = C3C & 0xcccccccc C4C = C4 ? C4 : C3C C5 = C4C & 0xaaaaaaaa
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 - AndrewOn 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…