在C位运算的数据结构++ [英] Structures with bitwise data in C++

查看:164
本文介绍了在C位运算的数据结构++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  转换位字段为int

我工作的一个应用程序,其中一部分处理包含了一些1位标志16位字。

I am working on an application, part of which handles 16-bit words that contain a number of 1-bit flags. I am handling the data using a structure similar to the one shown below:

struct mystruct
{
   uint16_t Reserved1   :3;
   uint16_t WordErr     :1;
   uint16_t SyncErr     :1;
   uint16_t WordCntErr  :1;
   uint16_t Reserved2   :10;
};

即。该结构包含作为许多较小的(在某些情况下,1位标志)个处理的单个16位变量中。

i.e. the structure contains a single 16-bit variable that is handled as a number of smaller (in some cases, 1-bit flag) pieces.

我的问题是这样的,有一个简单的方法来处理整个16位字作为一个数值,也就是说,将其输出到控制台或文件,或将其添加到另一个数据结构?我不知道这样做,除了转移的各个结构元素,并将它们添加到一个临时变量uint16_t任何方式。看来,有可能是提取整个字的简单的方法,但我不能找到编译器如何处理这样的结构的任何信息。

My question is this, is there a simple way to handle the entire 16-bit word as one value, say, to output it to the console, or a file, or add it to another data structure? I don't know of any way of doing this besides shifting the individual structure elements and adding them to a temporary uint16_t variable. It seems that there is probably a simpler way of extracting the entire word, but I can't find any information on how the compiler handles a structure like this.

编辑:我想这可能是显而易见的,但我试图简而言之做的是能够单独访问1位标志,以及使用这种结构类型uint16_t的一个变量(即无符号短,16位)。

I suppose this may be obvious but what I am trying to do in a nutshell is be able to access the 1-bit flags individually, as well as use the structure as a single variable of type uint16_t (i.e. unsigned short, 16 bits).

推荐答案

这里的标准方法是使用匿名结构/联合,这样的:

The standard approach here is to use anonymous structs/unions, like this:

union mystruct
{
   struct
   {
      uint16_t Reserved1   :3;
      uint16_t WordErr     :1;
      uint16_t SyncErr     :1;
      uint16_t WordCntErr  :1;
      uint16_t Reserved2   :10;
   };

   uint16_t word_field;
};

或者,如果工会不作为好一个顶级对象,

or, if union is not good as a top level object,

struct mystruct
{
   union
   {
       struct
       {
          uint16_t Reserved1   :3;
          uint16_t WordErr     :1;
          uint16_t SyncErr     :1;
          uint16_t WordCntErr  :1;
          uint16_t Reserved2   :10;
       };

       uint16_t word_field;
   };
};

这个定义允许内场的直接访问,如:

This definition allows direct access to the inner fields, like:

mystruct s1;
s1.WordCntErr = 1;

严格地说,编译器不给就如何结合不同的成员将互相重叠任何保证。它可以使用不同的路线,甚至转变。很多人在这里很容易指出这一点。然而,从实际情况来看看着这个,如果工会的各个领域有相同的大小,你可以放心地认为他们占据同一块内存。例如,code

Strictly speaking, compiler is not giving any guarantees on how different members of the union will overlap each other. It can use different alignments and even shifts. A lot of people here will readily point this out. Nevertheless, looking at this from the practical standpoint, if all fields of the union have the same size you can safely assume that they occupy the same piece of memory. For example, the code

s1.word_field = 0;

将归零所有位字段。 code吨使用的是这一点。这是不可想象的,这将不会停止工作。

will zero out all bit fields. Tons of code are using this. It is unthinkable this will ever stop working.

这篇关于在C位运算的数据结构++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆