Results 1 to 5 of 5

Thread: [RESOLVED] Bit-patterns.

Threaded View

  1. #1

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Resolved [RESOLVED] Bit-patterns.

    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:
    1. private ushort leftmost5bits(ushort v)
    2.         {
    3.             for (ushort p = 0x3e00; p >= 0x001f; p >>= 1) if ((p & v) == p) return p;
    4.  
    5.             return 0;
    6.         }


    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:
    CSharp Code:
    1. private byte countbits(ushort v)
    2.         {
    3.             return (byte)((v * 0x200040008001UL & 0x111111111111111UL) 'Modulus-operator' 0xf);
    4.         }
    EDIT: Above algorithm Sean Anderson + Bruce Dawson after original idea by Rich Schroeppel.
    I'm hoping something similar can be found / coded for the pattern-matching problem above.

    Thank you in advance for any input
    Thomas
    Last edited by ThomasJohnsen; Jun 17th, 2012 at 05:55 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width