简体   繁体   English

检查序列中的位模式

[英]Cheking a pattern of bits in a sequence

So basically i need to check if a certain sequence of bits occurs in other sequence of bits(32bits). 因此,基本上我需要检查某个位序列是否在其他位序列(32位)中出现。 The function shoud take 3 arguments: 该函数应采用3个参数:

  1. n right most bits of a value. n值的最右位。
  2. a value 一个值
  3. the sequence where the n bits should be checked for occurance 应检查n位出现的顺序

The function has to return the number of bit where the desired sequence started. 该函数必须返回所需序列开始的位数。 Example chek if last 3 bits of 0x5 occur in 0xe1f4. 如果在0xe1f4中出现了0x5的最后3位,则检查示例。

void bitcheck(unsigned int source, int operand,int n)
{
   int i,lastbits,mask;
   mask=(1<<n)-1;
   lastbits=operand&mask;

   for(i=0; i<32; i++)
   {
      if((source&(lastbits<<i))==(lastbits<<i))
          printf("It start at bit number %i\n",i+n);
   }
}

I take you to mean that you want to take source as a bit array, and to search it for a bit sequence specified by the n lowest-order bits of operand . 我的意思是说,您想将source作为位数组,并在其中搜索由noperand最低位指定的位序列。 It seems you would want to perform a standard mask & compare; 看来您想执行标准遮罩并进行比较; the only (minor) complication being that you need to scan. 唯一(较小)的并发症是您需要扫描。 You seem already to have that idea. 您似乎已经有了这个主意。

I'd write it like this: 我会这样写:

void bitcheck(uint32_t source, uint32_t operand, unsigned int n) {
    uint32_t mask = ~((~0) << n);
    uint32_t needle = operand & mask;
    int i;

    for(i = 0; i <= (32 - n); i += 1) {
        if (((source >> i) & mask) == needle) {
            /* found it */
            break;
        }
    }
}

There are some differences in the details between mine and yours, but the main functional difference is the loop bound: you must be careful to ignore cases where some of the bits you compare against the target were introduced by a shift operation, as opposed to originating in source , lest you get false positives. 我和你的细节之间有一些差异,但是主要的功能差异是循环界限:您必须小心忽略与目标进行比较的某些位是由移位操作而不是原始操作引入的情况从source上讲,免得误报。 The way I've written the comparison makes it clearer (to me) what the bound should be. 我编写比较的方式(对我而言)使界限更清楚。

I also use the explicit-width integer data types from stdint.h for all values where the code depends on a specific width. 我还将stdint.h的显式宽度整数数据类型用于代码依赖于特定宽度的所有值。 This is an excellent habit to acquire if you want to write code that ports cleanly. 如果要编写干净移植的代码,这是一个很好的习惯。

Perhaps: 也许:

if((source&(maskbits<<i))==(lastbits<<i))

Because: finding 10 in 11 will be true for your old code. 因为:在11中找到10对您的旧代码是正确的。 In fact, your original condition will always return true when 'source' is made of all ones. 实际上,当“ source”由所有条件组成时,您的原始条件将始终返回true。

Your loop goes too far, I'm afraid. 恐怕您的循环太快了。 It could, for example 'find' the bit pattern '0001' in a value ~0 , which consists of ones only. 它可以,例如,在值“发现”的位模式“0001” ~0 ,其中包括唯一的。

This will do better (I hope): 这样会更好(我希望):

void checkbit(unsigned value, unsigned pattern, unsigned n)
{
    unsigned size = 8 * sizeof value;
    if( 0 < n && n <= size)
    {
        unsigned mask = ~0U >> (size - n);
        pattern &= mask;

        for(int i = 0; i <= size - n; i ++, value >>= 1)
            if((value & mask) == pattern)
                printf("pattern found at bit position %u\n", i+n);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM