另请参阅问题末尾的更新...
鉴于以下情况:
[Flags]
enum SourceEnum
{
SNone = 0x00,
SA = 0x01,
SB = 0x02,
SC = 0x04,
SD = 0x08,
SAB = SA | SB,
SALL = -1,
}
[Flags]
enum DestEnum
{
DNone = 0x00,
DA = 0x01,
DB = 0x02,
DC = 0x04,
DALL = 0xFF,
}
Run Code Online (Sandbox Code Playgroud)
我想基于映射函数将一个枚举类型转换为另一个,反之亦然,使用像big switch()这样的名称,但由于这是一个标志枚举,我很难设计这样的例程是通用的.
基本上,我想要的是以下内容:
示例#1
SourceEnum source = SourceEnum.SA;
DestEnum dest = Map<Source, Dest> (source);
Assert.That (dest, Is.EqualTo (DestEnum.DA));
Run Code Online (Sandbox Code Playgroud)
例#2
SourceEnum source = SourceEnum.SA | SourceEnum.SB;
DestEnum dest = Map<Source, Dest> …Run Code Online (Sandbox Code Playgroud) 在这个链接上,我遇到了
http://lxr.linux.no/#linux+v2.6.36/include/linux/pci.h#L299
整数声明
unsigned int is_added:1;我已经制作了C程序并声明了整数,但在上面我看到了使用
:那
是什么语法?
例如:
struct a {
uint32_t foreColor_ : 32;
uint32_t backColor_ : 32;
uint16_t lfHeight_ : 16;
uint16_t flags_: 4;
bool lfBold_: 1;
bool lfItalic_: 1;
bool lfUnderLine_: 1;
bool lfDashLine_: 1;
bool lfStrike_: 1;
bool lfSubscript_: 1;
bool lfSuperscript_: 1;
};
Run Code Online (Sandbox Code Playgroud)
是16个字节但是
struct a {
uint32_t foreColor_ : 32;
uint32_t backColor_ : 32;
uint16_t lfHeight_ : 16;
uint8_t flags_: 4;
bool lfBold_: 1;
bool lfItalic_: 1;
bool lfUnderLine_: 1;
bool lfDashLine_: 1; //for ime
bool lfStrike_: 1;
bool lfSubscript_: 1; …Run Code Online (Sandbox Code Playgroud) 我试图定义一个bit10字节的字段。在SQL Server中,我将使用varbinary(10)。我知道可以bytea代替varbinary (MAX)图像,但是没有找到任何有关限制位数的文档。
有没有办法做到这一点?
我有一个由微控制器制造商提供的寄存器定义,可以作为位字段处理.寄存器定义如下:
#define SCU_WDTSCON0 (*( SCU_WDTSCON0_type *) 0xf00360f0u)
Run Code Online (Sandbox Code Playgroud)
位字段定义如下所示:
typedef volatile union {
unsigned U;
int I;
struct {
unsigned ENDINIT :1; // [0:0] End-of-Initialization Control Bit
unsigned LCK :1; // [1:1] Lock Bit to Control Access to WDTxCON0
unsigned HPW0 :2; // [3:2] Hardware Password 0
unsigned HPW1 :4; // [7:4] Hardware Password 1
unsigned PW :8; // [15:8] User-Definable Password Field for Access to WDTxCON0
unsigned REL :16; // [31:16] Reload Value for the WDT
} B;
} SCU_WDTSCON0_type; …Run Code Online (Sandbox Code Playgroud) 我有一个64位长的int,其中包含一些位域.我需要在第二个和第三个字节中存储一个16位有符号的int并将其添加到32位值.我正在使用这样的东西:
u32 Function( s32 value , u64 bitfield )
{
return value + (s16) (bitfield >> 8)
}
Run Code Online (Sandbox Code Playgroud)
我可以依赖编译器将位域转换为16位带符号的int,然后再将其扩展为32位带符号的int并执行添加吗?如果没有,我应该如何截断剩余的字节并执行我需要的类型转换?
在结构中读取pthread库,定义如下:
struct ptw32_thread_t_
{
....
int implicit:1;
......
};
Run Code Online (Sandbox Code Playgroud)
我知道它只占用1位然后如何给它赋值,因为激活溢出错误标志编译的每个值都会给出错误:
ptw32_thread_t *sp;
sp = (ptw32_thread_t *) calloc (1, sizeof(ptw32_thread_t));
sp->implicit = 1;
error: overflow in implicit constant conversion [-Werror=overflow]
Run Code Online (Sandbox Code Playgroud) 我有以下系列的结构.
struct FooWord1
{
unsigned int Fill : 8;
unsigned int someData1 : 18;
unsigned int someData2 : 6;
};
struct FooWord2
{
unsigned int Fill : 8;
union
{
unsigned int A_Bit : 1;
unsigned int B_Bit : 1;
};
unsigned int someData3 : 23;
};
struct Foo_Data
{
FooWord1 fooWord1;
FooWord2 fooWord2;
FooWord3 fooWord3; // similar to FooWord1
FooWord4 fooWord4; // similar to FooWord1
FooWord5 fooWord5; // similar to FooWord1
};
Foo_Data fooArray[SIZE];
Run Code Online (Sandbox Code Playgroud)
数据fooArray从网络消息逐字节复制.someData3如果我们不使用带有1位字段( …
在这里查看此问题的C版本。
当存在填充位时,我有两个关于位字段的问题。
说我有一个结构定义为
struct T {
unsigned int x: 1;
unsigned int y: 1;
};
Run Code Online (Sandbox Code Playgroud)
结构T仅实际使用了两位。
问题1:这两位始终是基础无符号int的最低有效位吗?还是取决于平台?
问题2:那些未使用的30位是否总是初始化为0?C ++标准对此有何评论?
我试图理解位域的概念.但我无法找到CASE III中以下结构的大小为8字节的原因.
struct B
{
unsigned char c; // +8 bits
} b;
Run Code Online (Sandbox Code Playgroud)
的sizeof(B); //输出:1(因为unsigned char在我的系统上占用1个字节)
struct B
{
unsigned b: 1;
} b;
sizeof(b); // Output: 4 (because unsigned takes 4 bytes on my system)
Run Code Online (Sandbox Code Playgroud)
struct B
{
unsigned char c; // +8 bits
unsigned b: 1; // +1 bit
} b;
sizeof(b); // Output: 8
Run Code Online (Sandbox Code Playgroud)
我不明白为什么案例III的输出为8.我期待1(char)+ 4(无符号)= 5.