这可能有点奇怪,但这是我遇到的问题。
我有一个这样的结构:
typedef struct smallStruct{
unsigned int First10 : 10;
unsigned int Next10 : 10;
unsigned int Third10 : 10;
unsigned int extra : 1;
} small
我需要能够传入一个 int 并将其设置为适合 First10 的 10 位,然后是 Next10 的另一个 int,等等。出现这种情况的原因是因为我需要能够在一个 4 字节的变量中存储 4 条不同的信息,这是我能想到的最有效的方法。
我需要为此进行位移吗?
最佳答案
你可以简单地赋值;位域逻辑负责必要的修改:
small make_small(int a, int b, int c) {
small result = { a, b, c, 0 };
return result;
}
void update_first(int value, small * s) {
s->First10 = value;
}
// etc.
关于c - 将 int 设置为特定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959776/