掌握位字段负值 [英] Getting negative value with bit-fields
本文介绍了掌握位字段负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有关于在C位字段的问题在这里,我有这样的结构:
结构注册
{
int位:1;
};INT主要(无效)
{
结构寄存器位= {1}; 的printf(\\ n位=%d个,bit.bit);
返回0;
}
能否请您解释一下我为什么我得到:
位= -1
块引用>解决方案如果你与位域工作,你应该使用
unsigned int类型
。符号int
是一个问题位字段。I have a question related to bit-fields in C. Here I have such a structure:
struct Register { int bit:1; }; int main(void) { struct Register bit = {1}; printf("\nbit = %d", bit.bit); return 0; }
Can you please explain me why do I get:
bit = -1
解决方案If you're working with bitfields, you should use
unsigned int
.signed int
is a problem for bit-fields.这篇关于掌握位字段负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文