简体   繁体   English

我对bitmasks感到困惑

[英]I'm confused with bitmasks

I am using IAR embedded workbench and C. I would like to create a mask and decide whether next bit for transmit is 1 or 0. 我正在使用IAR嵌入式工作台和C.我想创建一个掩码并确定传输的下一位是1还是0。

I tried this but didn't work 我尝试了这个,但没有奏效

int transmit(int signal, int number_of_bits)
{

    int mask;
    for (int i = 0; i < number_of_bits; i++)
    {
        mask = pow(2,number_of_bits-1-i)
        if ((signal & mask) == 0) // bit '0'
        {
            transmit0();
        }
        else // bit '1'
        {
            transmit1();
        }
    }
}

And I tried this, it tranmits 0001 but i'm trying to transmit 1000 (vice versa) 我尝试了这个,它传输0001但我试图传输1000(反之亦然)

int transmit(int signal, int number_of_bits)
{

    int mask;
    for (int i = 0; i < number_of_bits; i++)
    {
        mask = (1 << i);
        if ((signal & mask) == 0) // bit '0'
        {
            transmit0();
        }
        else // bit '1'
        {
            transmit1();
        }
    }
}

正如@LF注意到的那样, ^不是幂运算符,唉<< ,按位移位运算符,就可以了。

Here mask is the variable that will always contain a "power of 2" value. 这里mask是一个总是包含“2的幂”值的变量。 The goal is to only have one bit set in the mask at all times. 目标是始终只在mask中设置一个位。 This mask is used to test each bit in "isolation". mask用于测试“隔离”中的每个位。

In combination with the logical AND -function, you can see it as a filter that gives you only the value that particular bit corresponds to or nothing (zero). 结合逻辑AND功能,您可以将其视为一个过滤器,仅为您提供特定位对应的值或零(零)。

Remember that the value of a binary word with only one bit set always corresponds to a power 2 value by definition. 请记住,根据定义,仅设置一位的二进制字的值始终对应于幂2值。 So after the test of a single bit you will see either zero or a power of 2 value. 因此,在测试单个位后,您将看到零或2的幂值。

Let's look at some practical values while the program is running (assuming 8 bits). 让我们看一下程序运行时的一些实用值(假设为8位)。 So in the for loop, this is what the value of mask looks like over time: 所以在for循环中,这就是mask随时间变化的值:

00000001
00000010
00000100
00001000
00010000
...

Note: you can also go into the reverse direction, for instance if the receiver wants to see the highest bit first and the lowest last. 注意:您也可以进入相反的方向,例如,如果接收器想要先看最高位,最后看最低位。 Don't know what is needed here, but just so you know. 不知道这里需要什么,但你知道。 If you want to go in reverse, you initialize mask with (1 << (num_bits-1)) then go one step to the right every time in the loop itself mask >>= 1 ). 如果你想反过来,你用(1 << (num_bits-1))初始化mask然后每次在循环本身mask >>= 1 )向右走一步。

Every mask value is bitwise AND -ed ( & operator) with the input value signal to "filter" out that one particular bit we're interested in. 每个掩码值与输入值signal按位AND运算( &运算符),以“滤除”我们感兴趣的一个特定位。

Remember: the outcome of this AND operation is still multiple bits -- not a single bit, so we can't look for a value of 1 (we'd also have to test for the cases 2, 4, 8 ... etc.). 记住:这个AND运算的结果仍然是多位 - 不是一位,所以我们不能找到值1(我们还必须测试情况2,4,8 ......等等)。

Example, so let's say all the bits are on, then the logical AND output will look like this: 例如,假设所有位都打开,那么逻辑AND输出将如下所示:

00000001 AND 11111111 = 00000001
00000010 AND 11111111 = 00000010
00000100 AND 11111111 = 00000100
00001000 AND 11111111 = 00001000
00010000 AND 11111111 = 00010000
...

Note how the outcome follows the mask in this case. 请注意在这种情况下结果如何跟随掩码。 Although the values differ every time, you know that when you got something nonzero, the bit you are testing must be high! 虽然这些值每次都不同,但是你知道当你得到非零的东西时,你测试的那个位必须很高!

And if all the bits are off you will see this: 如果所有位都关闭,您将看到:

00000001 AND 00000000 = 00000000
00000010 AND 00000000 = 00000000
00000100 AND 00000000 = 00000000
00001000 AND 00000000 = 00000000
00010000 AND 00000000 = 00000000

You will note it's easier to check if the result is zero. 您会注意到检查结果是否为零更容易。 That means the bit was off. 这意味着该位已关闭。 It's also why the code only checks for a value of zero, it knows the bit is 1 in all the other cases! 这也是代码只检查零值的原因,它知道在所有其他情况下该位为1

Now the trick is to get the correct mask value. 现在的诀窍是获得正确的掩码值。 What you want is shift a bit value of 1 left to the correct place. 你需要的是转变位值的1留给了正确的位置。 So if you start testing for bit 4 for instance, you'd shift 1 four places to the left and you will get (00010000 binary). 因此,如果您开始测试第4位,您将向左移动4个位置,您将获得(00010000二进制)。

For this we use the bit shift left operator << . 为此,我们使用位移左运算符<<

As others have already said, ^ unfortunately does something completely different (XOR), which adds to the whole confusion. 正如其他人已经说过的那样, ^不幸的是做了一些完全不同的事情(XOR),这增加了整个混乱。

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

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