Fastest way to scan for bit pattern in a stream of bits

后端 未结 13 1752
悲哀的现实
悲哀的现实 2020-12-07 13:38

I need to scan for a 16 bit word in a bit stream. It is not guaranteed to be aligned on byte or word boundaries.

What is the fastest way of achieving this

13条回答
  •  攒了一身酷
    2020-12-07 14:35

    You can use the fast fourier transform for extremely large input (value of n) to find any bit pattern in O(n log n ) time. Compute the cross-correlation of a bit mask with the input. Cross -correlation of a sequence x and a mask y with a size n and n' respectively is defined by

    R(m) = sum  _ k = 0 ^ n' x_{k+m} y_k
    

    then occurences of your bit pattern that match the mask exactly where R(m) = Y where Y is the sum of one's in your bit mask.

    So if you are trying to match for the bit pattern

    [0 0 1 0 1 0]
    

    in

    [ 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1]
    

    then you must use the mask

    [-1 -1  1 -1  1 -1]
    

    the -1's in the mask guarantee that those places must be 0.

    You can implement cross-correlation, using the FFT in O(n log n ) time.

    I think KMP has O(n + k) runtime, so it beats this out.

提交回复
热议问题