2 bits size variable

二次信任 提交于 2019-12-18 06:50:45

问题


I need to define a struct which has data members of size 2 bits and 6 bits. Should I use char type for each member?Or ,in order not to waste a memory,can I use something like :2\ :6 notation? how can I do that? Can I define a typedef for 2 or 6 bits type?


回答1:


You can use something like:

typedef struct {
    unsigned char SixBits:6;
    unsigned char TwoBits:2;
} tEightBits;

and then use:

tEightBits eight;
eight.SixBits = 31;
eight.TwoBits = 3;

But, to be honest, unless you're having to comply with packed data external to your application, or you're in a very memory constrained situation, this sort of memory saving is not usually worth it. You'll find your code is a lot faster if it's not having to pack and unpack data all the time with bitwise and bitshift operations.


Also keep in mind that use of any type other than _Bool, signed int or unsigned int is an issue for the implementation. Specifically, unsigned char may not work everywhere.




回答2:


It's probably best to use uint8_t for something like this. And yes, use bit fields:

struct tiny_fields
{
  uint8_t twobits : 2;
  uint8_t sixbits : 6;
}

I don't think you can be sure that the compiler will pack this into a single byte, though. Also, you can't know how the bits are ordered, within the byte(s) that values of the the struct type occupies. It's often better to use explicit masks, if you want more control.




回答3:


Personally I prefer shift operators and some macros over bit fields, so there's no "magic" left for the compiler. It is usual practice in embedded world.

#define SET_VAL2BIT(_var, _val) ( (_var) | ((_val) & 3) )
#define SET_VAL6BIT(_var, _val) ( (_var) | (((_val) & 63)  << 2) )

#define GET_VAL2BIT(_var) ( (_val) & 3)
#define GET_VAL6BIT(_var) ( ((_var) >> 2) & 63 )

static uint8_t my_var;

<...>
SET_VAL2BIT(my_var, 1);
SET_VAL6BIT(my_var, 5);
int a = GET_VAL2BIT(my_var); /* a == 1 */
int b = GET_VAL6BIT(my_var); /* b == 5 */


来源:https://stackoverflow.com/questions/14600153/2-bits-size-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!