函数接受两个参数,一个字节和一个位字段,并返回字节中字段的值

发布于 2024-11-15 05:20:07 字数 230 浏览 0 评论 0原文

我在网上找到了一些执行此任务的代码:

byte = byte >> field;
byte = byte & 0x01;
return(byte);

但是,我不明白为什么我们不能这样做:

return(byte & field);

这会起作用吗?为什么或为什么不呢?有更好的实现吗?

I found some code online that performs this task:

byte = byte >> field;
byte = byte & 0x01;
return(byte);

However, I don't understand why we can't just do this:

return(byte & field);

Will this work? Why or why not? Are there better implementations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光下的泡沫是彩色的 2024-11-22 05:20:08

这可以通过结构给出。可以说:

struct POWERTRAIN_ERROR 
{

    uint8 ERROR_CODE;

    unit8 LAMP_STATUS : 1;
    };

    struct POWERTRAIN_ERROR   pt_error;

    uint8 func ( struct POWERTRAIN_ERROR pt)

    {

    // do something with pt.ERROR_CODE (which is a byte) and pt.LAMP_STATUS which is a bit field

    // lets say, this function needs to return the status of 0th bit of ERROR_CODE 

    return ( pt.ERROR_CODE & 0x1) ;

}

This can be given by a structure. Lets say:

struct POWERTRAIN_ERROR 
{

    uint8 ERROR_CODE;

    unit8 LAMP_STATUS : 1;
    };

    struct POWERTRAIN_ERROR   pt_error;

    uint8 func ( struct POWERTRAIN_ERROR pt)

    {

    // do something with pt.ERROR_CODE (which is a byte) and pt.LAMP_STATUS which is a bit field

    // lets say, this function needs to return the status of 0th bit of ERROR_CODE 

    return ( pt.ERROR_CODE & 0x1) ;

}
前事休说 2024-11-22 05:20:07

第一个相当于:

return (byte >> field) & 0x01;

它真正做的是转移到位置 field 的位,并返回 1 如果该位被设置,0否则。

您提出的那个是不正确的,因为它没有移动到指定字段的偏移量。例如,byte & 5 没有任何意义。

该函数也可以这样写:

return byte & (1 << field);

当然,如果你打算传递 1 << 5 而不是 5,您可以按照自己的方式编写。

我认为 field 是一个数字,指示我们感兴趣的位的位置,因此对于单个字节,它将在 0..7 范围内。

The first one is equivalent to:

return (byte >> field) & 0x01;

What it really does is shift to the bit with position field and return 1 if that bit is set, 0 otherwise.

The one you propose is incorrect because it doesn't shift to the offset of the designated field. For example, byte & 5 doesn't make any sense.

The function can as well be written like this:

return byte & (1 << field);

Of course, if you intend to pass 1 << 5 to it instead of 5, you can write it your way.

I suppose field is a number that indicates the position of the bit we are interested in, so for a single byte it would be in the range 0..7.

您的好友蓝忘机已上羡 2024-11-22 05:20:07

在第一个代码示例中,field 是您想要其值的字段中的位的位置。

在第二个示例中,字段必须是该位设置为 1 的 int,即 1 <<字段

In the first code sample, field is the position of the bit in the field of which you want the value.

In the second sample, field would have to be an int with that bit set to one, i.e. 1 << field.

享受孤独 2024-11-22 05:20:07

如果您确实希望返回值为零或一,则如果您只关心返回值为零或非零,则可以

return ((byte & (1 << field)) != 0);

return ((byte >> field) & 0x01);

两种形式简化一点。

If you really want the return value to be zero or one you can either

return ((byte & (1 << field)) != 0);

or

return ((byte >> field) & 0x01);

Both forms simplify a bit if all you care about is that the return value is zero or non-zero.

梦途 2024-11-22 05:20:07

右移字节

字段表示右侧字的位数 (LSB)。 字节=字节>> field 将带来 LSB 位置处的字节 byte 的位数 field。然后byte = byte & 0x01 和带有 0x01 的字节,这意味着如果在位号字段<中设置了位,则结果的 LSB 中将包含 1 /code> 最初,或者如果在该位置清除了该位,则将包含 0

例如,需要检查字节 0x52 是否已设置其位数 4 的测试如下所示。

    byte   = 0x52 =  0 1 0 1 0 0 1 0

    field  = 0x04 =  0 0 0 0 0 1 0 0

    Operation: byte = byte >> field

    The bit number 4 is single quoted below. Note how it moves

                                  intermediate byte       | lost bits during
                                        states            | right shifting

    byte         = 0x52         = 0  1  0 '1' 0  0  1  0  |
    shift 1      = 0x29         = 0  0  1  0 '1' 0  0  1  | 0
    shift 2      = 0x14         = 0  0  0  1  0 '1' 0  0  | 1 0
    shift 3      = 0x0A         = 0  0  0  0  1  0 '1' 0  | 0 1 0
    shift 4      = 0x05         = 0  0  0  0  0  1  0 '1' | 0 0 1 0

    Note that the bit 4 is now moved at the LSB/righ most position of the byte
    now if we test the rightmost position of the above byte then we can check
    if the bit number 4 had its bit set or cleared, with the following operation

    Operation: byte = byte & 0x01

    byte is now 0x05

    byte         = 0x05  = 0 0 0 0 0 1 0 '1'
    AND                    & & & & & & &  &
                   0x01  = 0 0 0 0 0 0 0  1
                   ----    ----------------
                   0x01    0 0 0 0 0 0 0  1

   Now byte contains 0x01 so bit number 4 had the bit set. In the other case the
   final answer would be 0.

但我们不能byte & field 检查编号为 field 的位是否被设置或清除。这是因为 field 只是一个二进制文件,而不是一个掩码。如果我们这样做 byte &字段 那么就会发生以下情况。

  byte         = 0x52  = 0 1 0 1 0 0 1 0
  AND                    & & & & & & & &
  field        = 0x04  = 0 0 0 0 0 1 0 0
                 ----    ---------------
                 0x00    0 0 0 0 0 0 0 0

字段的值为0x04,即设置的位数2。通过此操作,我们实际上检查了位号 2 是否已设置。如果field的值为5,那么位02将被设置,所以像上面一样直接进行AND运算如果提取 byte 值中的位 02 的状态,则会产生四种可能的组合。

将 0x01 向左移位并制作掩码

测试 byte 的位值的其他方法是不是移位 byte 本身,而是移位 0x01field 向左掩码一次,并将其与字节进行 AND,并检查是否为零。当 field 编号位被设置时,(byte & (0x01 << field)) != 0 将为 true,否则为 false。

   Operation: (0x01 << field)

      Shifting 0x01 to the left field times field = 0x04 for the example

                 = 0x01             = 0 0 0 0 0 0 0 1  
    shift 1      = 0x02             = 0 0 0 0 0 0 1 0
    shift 2      = 0x04             = 0 0 0 0 0 1 0 0
    shift 3      = 0x08             = 0 0 0 0 1 0 0 0
    shift 4      = 0x10             = 0 0 0 1 0 0 0 0


      After the left shift the '1' moves in the bit position 4
      Now we AND this with the byte to check if the bit position 4
      is set or clear.

  byte            = 0x52  = 0 1 0 1 0 0 1 0
  AND                       & & & & & & & &
  (0x01 << field) = 0x10  = 0 0 0 1 0 0 0 0
                    ----    ---------------
                    0x10    0 0 0 1 0 0 0 0

  Therefore the answer (0x01 != 0) is 1 there fore the bit 4 is set. It the bit 4
  was not set then the answer would be 0.

使用预计算掩码

如果您有一个需要定期测试的特定格式的字节,例如某些预定义字的某些位字段,其中每个位都表示一些特定的事物,那么您可以保留预计算掩码,它仅在特定的情况下有一个将使用该掩码测试的位位置。例如,要检查一个字节,预计算机掩码将是:

#define BIT_0 0x01 //(00000001)
#define BIT_1 0x02 //(00000010)
#define BIT_2 0x04 //(00000100)
#define BIT_3 0x08 //(00001000)
#define BIT_4 0x10 //(00010000)
#define BIT_5 0x20 //(00100000)
#define BIT_6 0x40 //(01000000)
#define BIT_7 0x80 //(10000000)

因此,要测试 byte 中的位 4,我们必须执行 return (byte & BIT_4)return (byte & BIT_4) != 0;

根据位位置表示的宏名称可以设置。

Shift byte to right

field represents the bit number of the word you have from the right hand side (LSB). byte = byte >> field will bring the bit number field of the byte byte at the LSB position. Then byte = byte & 0x01 ands the byte with 0x01 that means the result will contain a 1 in the LSB if it had the bit set in the bit number field originally, or will contain a 0 if it had the bit cleared at that position.

For example the test needed to check if the byte 0x52 has its bit number 4 set or not is shown below.

    byte   = 0x52 =  0 1 0 1 0 0 1 0

    field  = 0x04 =  0 0 0 0 0 1 0 0

    Operation: byte = byte >> field

    The bit number 4 is single quoted below. Note how it moves

                                  intermediate byte       | lost bits during
                                        states            | right shifting

    byte         = 0x52         = 0  1  0 '1' 0  0  1  0  |
    shift 1      = 0x29         = 0  0  1  0 '1' 0  0  1  | 0
    shift 2      = 0x14         = 0  0  0  1  0 '1' 0  0  | 1 0
    shift 3      = 0x0A         = 0  0  0  0  1  0 '1' 0  | 0 1 0
    shift 4      = 0x05         = 0  0  0  0  0  1  0 '1' | 0 0 1 0

    Note that the bit 4 is now moved at the LSB/righ most position of the byte
    now if we test the rightmost position of the above byte then we can check
    if the bit number 4 had its bit set or cleared, with the following operation

    Operation: byte = byte & 0x01

    byte is now 0x05

    byte         = 0x05  = 0 0 0 0 0 1 0 '1'
    AND                    & & & & & & &  &
                   0x01  = 0 0 0 0 0 0 0  1
                   ----    ----------------
                   0x01    0 0 0 0 0 0 0  1

   Now byte contains 0x01 so bit number 4 had the bit set. In the other case the
   final answer would be 0.

But we cannot do byte & field to check if the bit which is numbered field is set or cleared. This is because field is simply a binary and not a mask. If we did byte & field then the following would happen.

  byte         = 0x52  = 0 1 0 1 0 0 1 0
  AND                    & & & & & & & &
  field        = 0x04  = 0 0 0 0 0 1 0 0
                 ----    ---------------
                 0x00    0 0 0 0 0 0 0 0

The field has the value 0x04 which is, its bit number 2 set. With this operation we have checked actually if the bit number 2 is set. If the value of field was 5 then bit 0, and 2 would be set, so ANDing directly like above would result if extraction of the state of bit 0 and 2 in the value of byte, which can take four possible combination.

Shift 0x01 to left and make mask

Other ways to test the bit value of a byte is instead of shifting the byte itself, we shift the 0x01 mask field times to the left, and AND it with the byte, and check if is zero or not. (byte & (0x01 << field)) != 0 will be true when the field numbered bit is set or false otherwise.

   Operation: (0x01 << field)

      Shifting 0x01 to the left field times field = 0x04 for the example

                 = 0x01             = 0 0 0 0 0 0 0 1  
    shift 1      = 0x02             = 0 0 0 0 0 0 1 0
    shift 2      = 0x04             = 0 0 0 0 0 1 0 0
    shift 3      = 0x08             = 0 0 0 0 1 0 0 0
    shift 4      = 0x10             = 0 0 0 1 0 0 0 0


      After the left shift the '1' moves in the bit position 4
      Now we AND this with the byte to check if the bit position 4
      is set or clear.

  byte            = 0x52  = 0 1 0 1 0 0 1 0
  AND                       & & & & & & & &
  (0x01 << field) = 0x10  = 0 0 0 1 0 0 0 0
                    ----    ---------------
                    0x10    0 0 0 1 0 0 0 0

  Therefore the answer (0x01 != 0) is 1 there fore the bit 4 is set. It the bit 4
  was not set then the answer would be 0.

Use precomputed mask

If you have a byte with certain format which you need to test regularly, for example some bitfield of some pre defined word, where each bit means some specific thing then you can keep precomputer mask, which has a one only in the specific bit position which will be tested with that mask. For example to check for one byte the precomputer masks would be:

#define BIT_0 0x01 //(00000001)
#define BIT_1 0x02 //(00000010)
#define BIT_2 0x04 //(00000100)
#define BIT_3 0x08 //(00001000)
#define BIT_4 0x10 //(00010000)
#define BIT_5 0x20 //(00100000)
#define BIT_6 0x40 //(01000000)
#define BIT_7 0x80 //(10000000)

So to test the bit 4 in byte we have to do return (byte & BIT_4) or return (byte & BIT_4) != 0;

Depending on what the bit position represents the name of the macro could be set.

南七夏 2024-11-22 05:20:07

第一个示例将所需的位移动到返回的 LSB 中,但第二个示例只是屏蔽掉所有不需要的位。

The 1st example moves the required bit into the LSB of the return but the second example simply masks out all the bits that are not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文