在32位字位模式中找到"边"

Eri*_*rik 2 c c++ bit-manipulation

我试图找到最有效的算法来计算位模式中的"边缘".边缘意味着从0变为1或1变为0.我每250 us对每个位进行采样并将其转换为32位无符号变量.

到目前为止,这是我的算法

void CountEdges(void)
{
    uint_least32_t feedback_samples_copy = feedback_samples;
    signal_edges = 0;

    while (feedback_samples_copy > 0)
    {
        uint_least8_t flank_information = (feedback_samples_copy & 0x03);

        if (flank_information == 0x01 || flank_information == 0x02)
        {
            signal_edges++;
        }

        feedback_samples_copy >>= 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

它需要至少2或3倍的速度.

Isa*_*avo 6

您应该能够将它们按位进行异或,以获得表示翻转位的位模式.然后使用此页面上的一个位计数技巧:http://graphics.stanford.edu/~seander/bithacks.html来计算结果中有多少1个.