我有一些结构包含一个位域,其大小可能会有所不同.例:
struct BitfieldSmallBase {
uint8_t a:2;
uint8_t b:3;
....
}
struct BitfieldLargeBase {
uint8_t a:4;
uint8_t b:5;
....
}
Run Code Online (Sandbox Code Playgroud)
和一个联合访问所有位一次:
template<typename T>
union Bitfield
{
T bits;
uint8_t all; // <------------- Here is the problem
bool operator & (Bitfield<T> x) const {
return !!(all & x.all);
}
Bitfield<T> operator + (Bitfield<T> x) const {
Bitfield<T> temp;
temp.all = all + x.all; //works, because I can assume no overflow will happen
return temp;
}
....
}
typedef Bitfield<BitfieldSmallBase> BitfieldSmall; …Run Code Online (Sandbox Code Playgroud) 所以我在我们的应用程序中转换一些位域来代替使用EnumSet,我很好奇是否有更好的方法来对X | Y进行比较.目前我们做的事情如下:
if(bitfield & (X | Y) != 0) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
EnumSet等效似乎是:
if(enumSet.contains(X) || enumSet.contains(Y)) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
有更清洁的方法吗?我知道你可以这样检查containsAll():
EnumSet flagsToCheck = EnumSet.of(X, Y);
if(enumSet.containsAll(flagsToCheck)) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
但这是一个你想知道是否(X & Y)已设置的场景.有没有相同的方法来检查(X | Y)?我认为会有类似于containsAny()方法的东西,但我看不到任何似乎有这种效果的东西.
C中未命名的位域有什么用?
示例:
typedef struct fun {
unsigned int :8;
unsigned int foo1 :1;
unsigned int foo2 :1;
unsigned int foo3 :1;
unsigned int foo4 :1;
unsigned int foo5 :1;
}dig;
Run Code Online (Sandbox Code Playgroud)
有什么用途:
unsigned int :8;
Run Code Online (Sandbox Code Playgroud) unsigned int x = 0xdeadbeef;
unsigned int y = 0x00000006;
unsigned int z = 0xdeadbee7;
Run Code Online (Sandbox Code Playgroud)
如何计算值z从价值观x和y?
第1-3位y是011,我希望将值1-3中的x位1-3替换为第1-3位,y将其他位保持原样.
我期待1作为输出..
#include<stdio.h>
int main(){
struct A{
int a:1;
};
struct A bb;
bb.a=1;
printf("%d",bb.a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个与C中的位字段有关的问题.这里有一个结构:
struct Register
{
int bit:1;
};
int main(void)
{
struct Register bit = {1};
printf("\nbit = %d", bit.bit);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你能解释一下我为什么得到:
bit = -1
我最近玩C中的位字段,显然,这些位从低到高排序,尽管谷歌不支持这一论点(例如维基百科).
请参考以下代码(http://ideone.com/UwWfJM):
#include <stdio.h>
struct bits {
unsigned char a : 1;
unsigned char b : 1;
unsigned char c : 1;
unsigned char d : 1;
unsigned char e : 1;
unsigned char f : 1;
unsigned char g : 1;
unsigned char h : 1;
};
int main(int argc, char **argv)
{
unsigned char c = 33;
struct bits *b = (struct bits *) &c;
printf("dec: %u\n", c);
printf("bits: %x", b->a);
printf("%x", b->b);
printf("%x", …Run Code Online (Sandbox Code Playgroud) 所以我知道
struct bit
{
unsigned char a : 1;
}
Run Code Online (Sandbox Code Playgroud)
由于填充,仍会占用一个字节,我的问题是:
struct bit
{
unsigned char a : 1;
...
unsigned char h : 1;
}
Run Code Online (Sandbox Code Playgroud)
这个结构会占用与char相同的大小吗?如果是这样,我最好只使用一个char而不是?我问,因为我想使用位作为键,但如果可能的话,我宁愿避免按位操作.
我在使用结构的C中遇到了这个问题.我不确定这里到底发生了什么.谢谢
#include<stdio.h>
int main()
{
struct num1
{
int n1:2;
int n2:3;
int n3:4;
};
struct num1 num={3,4,5};
printf("%d %d %d\n",num.n1,num.n2,num.n3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
获得的输出是
-1 -4 5
在c/c ++中,我们可以在内存中定义一个1位的变量:如unsigned char value : 1;
有没有办法声明一个1位元素的数组?比如下面的sudo代码:
unsigned char : 1 data[10];
Run Code Online (Sandbox Code Playgroud)