为什么我们没有 4 位大小的数据类型?如果我们如此倾向,为什么我们不能制造它们?我见过位域,但我听说它们不可移植,也许也不常用?我认为这是机器如何解释位位置的位置值的结果。(大端,小端)
typedef struct
{
int b1 : 1;
int b2 : 1;
..
..
..
int b32 : 1;
} bitfield32;
Run Code Online (Sandbox Code Playgroud)
我们也不能制作这样一个比任何原始类型都大的位域。那么为什么要限制呢?可以在组装中完成吗?
我有这个代码 - http://ideone.com/sXhWxf
#include <stdio.h>
int main(void) {
struct bitfield{
unsigned a:5;
unsigned c:5;
unsigned b:6;
} bit = {1,3,3};
char *p = (char*)&bit;
printf("%d\n",*p);
p++;
printf("%d\n",*p);
// I assumed that the bits are laid out in the below order in the memory.
// Spaces are just for clarity
// 00001 00011 000011
// Also, I asumed that the 'char' will take 8 bits. But I can't understand output.
// According to me the output should be - 8 195 …Run Code Online (Sandbox Code Playgroud) 我将如何构造OptionSetType一个原始值大于 64 位移位(即Int64)仍然可以使用编码的NSCoder?我有超过 64 个潜在的按位选项可以组合。
我正在尝试编写一个辅助函数,它将把位索引数组转换为符合 OptionSet 的类。
func getOptionSet<T: OptionSet>(bitIndexes: [Int64]) -> T {
var result: Int64 = 0
for index in bitIndexes {
result |= 1 << index
}
return T(rawValue: result) // error
}
Run Code Online (Sandbox Code Playgroud)
这无法编译:
Cannot invoke initializer for type 'T' with an argument list of type '(rawValue: Int64)'
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 RawValue:
func getOptionSet<T: OptionSet>(bitIndexes: [T.RawValue]) {
var result = T.RawValue() // error
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
Cannot invoke value of type 'T.RawValue.Type' with argument list '()'
Run Code Online (Sandbox Code Playgroud)
这可以做到吗?我需要对 T 添加额外的约束吗?
我知道可以重写这个函数以使用具体类型,但如果可能的话我想保持它的通用性。
我有一个struct喜欢以下内容:
struct Foo {
unsigned int id;
unsigned int flag_1 : 1;
unsigned int flag_2 : 1;
unsigned int flag_3 : 1;
// Some arbitrary number of further flags. Code is
// automatically generated and number will vary.
// Notably, it may be more than an int's worth.
int some_data;
float some_more_data;
// ...
};
Run Code Online (Sandbox Code Playgroud)
有时,我需要将所有标志重置为零,同时保留结构的其余部分。一种方法显然是将每个标志单独设置为 0,但感觉应该有一种方法可以一举完成。那可能吗?
(请注意,我愿意不使用位域,但这些代码有时会在内存受限的系统上运行,因此节省的内存非常吸引人。)
编辑:
这里有一个类似的问题:Reset all bits in ac bitfield
但是,该问题中的结构完全是位域。我不能memset在这里简单地将整个结构归零,并且不能保证涉及联合的其他答案有效,尤其是当标志的价值超过 int 时。
GNU C 有两个扩展,它建议制作安全的宏,MAX并且MIN只对参数求值一次:typeof和__auto_type. 给出两个MAX宏的例子来演示每个宏:
#define MAX(a, b) ({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; \
})
#define MAX(a, b) ({ \
__auto_type _a = (a); \
__auto_type _b = (b); \
_a > _b ? _a : _b; \
})
Run Code Online (Sandbox Code Playgroud)
对于这两个问题是,typeof并__auto_type给出错误,如果它是对位的领域。此示例代码显示了使用位字段的问题MAX:
#include <stdio.h>
#include <stdint.h>
// Insert one of the MAX macros …Run Code Online (Sandbox Code Playgroud) 我编写了这个程序作为 C++ 中位域成员比较行为的测试用例(我想同样的行为也会在 C 中表现出来):
#include <cstdint>
#include <cstdio>
union Foo
{
int8_t bar;
struct
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
int8_t baz : 1;
int8_t quux : 7;
#elif __BYTE_ORDER == __BIG_ENDIAN
int8_t quux : 7;
int8_t baz : 1;
#endif
};
};
int main()
{
Foo foo;
scanf("%d", &foo.bar);
if (foo.baz == 1)
printf("foo.baz == 1\n");
else
printf("foo.baz != 1\n");
}
Run Code Online (Sandbox Code Playgroud)
在我将其1作为输入编译并运行后,我得到以下输出:
foo.baz != 1
*** stack smashing detected ***: terminated
fish: “./a.out” terminated by signal SIGABRT …Run Code Online (Sandbox Code Playgroud) #include <iostream>
typedef union dbits {
double d;
struct {
unsigned int M1: 20;
unsigned int M2: 20;
unsigned int M3: 12;
unsigned int E: 11;
unsigned int s: 1;
};
};
int main(){
std::cout << "sizeof(dbits) = " << sizeof(dbits) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:sizeof(dbits) = 16,但是如果
typedef union dbits {
double d;
struct {
unsigned int M1: 12;
unsigned int M2: 20;
unsigned int M3: 20;
unsigned int E: 11;
unsigned int s: 1;
};
};
Run Code Online (Sandbox Code Playgroud)
输出: …
我正在阅读有关位域的维基百科条目,并了解如何使用二进制数来表示原色并使用按位 OR ( |) 运算符将它们组合起来。我想检查一种颜色是否包含在另一种颜色中。
#include <stdio.h>
// primary colors
#define RED 0b001
#define GREEN 0b010
#define BLUE 0b100
// mixed colors
#define BLACK 0b000
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (BLUE | GREEN)
#define WHITE (RED | GREEN | BLUE)
int main(void)
{
int magenta = MAGENTA;
int blue = BLUE;
#define in & // check if a color A is contained in another color B:
printf("%s\n", blue in magenta ? …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main()
{
struct s{
int bit_fld:3;
};
s a;
a.bit_fld=0x10;
a.bit_fld =( a.bit_fld | (1<<2));
printf("%x\n",a.bit_fld);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该计划输出fffffffc.
我试图手动计算输出,我无法获得编译器生成的输出.
bit_fld = 00010000 and (1<<2) = 0100oring两个结果00010100都是0x14十六进制的.为什么我对输出的看法是错误的?帮助我理解我错在哪里.