Hello forum-members.
I'm currently in need of a (very) fast way of determining, if a 14 bit unsigned integer contains 5 consequtive set bits. Only the leftmost, if any, such pattern need be detected. I have ofc implemented the naive approach, but there may be a faster version that can be implemented.
Current code:
CSharp Code:
private ushort leftmost5bits(ushort v) { for (ushort p = 0x3e00; p >= 0x001f; p >>= 1) if ((p & v) == p) return p; return 0; }
For instance when counting set bits in a 14-bit number, I came across this excellent little algoritm, that does significantly better than the naive approach:
EDIT: Above algorithm Sean Anderson + Bruce Dawson after original idea by Rich Schroeppel.CSharp Code:
private byte countbits(ushort v) { return (byte)((v * 0x200040008001UL & 0x111111111111111UL) 'Modulus-operator' 0xf); }
I'm hoping something similar can be found / coded for the pattern-matching problem above.
Thank you in advance for any input
Thomas




Reply With Quote