bit-fields

Struct variable doesn't changed by assignment

无人久伴 提交于 2019-12-03 00:16:30
问题 struct st { int a1 : 3; int a2 : 2; int a3 : 1; } void main(void) { x.a3 = -1; if (x.a3 == -1) printf("TRUE\n"); else printf("FALSE\n"); x.a3 = 1; if (x.a3 == 1) printf("TRUE\n"); else printf("FALSE\n"); } In case, 'x.a3 = -1;' First if is TRUE . But, why 'x.a3 = 1' doesn't changed in second if ? It's still x.a3 = -1. And If I type 'x.a3 = 1;' in first if, it still x.a3 = = 1 !! It doesn't changed! Debug Result in XCode 回答1: The problem is, a signed 1 bit variable can hold only two values, -1

Packed bit fields in c structures - GCC

和自甴很熟 提交于 2019-12-02 22:47:50
I am working with structs in c on linux. I started using bit fields and the "packed" attribute and I came across a wierd behavior: struct t1 { int a:12; int b:32; int c:4; }__attribute__((packed)); struct t2 { int a:12; int b; int c:4; }__attribute__((packed)); void main() { printf("%d\n",sizeof(t1)); //output - 6 printf("%d\n",sizeof(t2)); //output - 7 } How come both structures - that are exactly the same - take diffrent number of bytes? Your structures are not "exactly the same". Your first one has three consecutive bit-fields, the second has one bit-field, an (non bit-field) int, and then

In C, what does a colon mean inside a declaration? [duplicate]

五迷三道 提交于 2019-12-02 20:02:51
This question already has answers here : What does 'unsigned temp:3' in a struct or union mean? [duplicate] (4 answers) Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came along the following line (in linux 2.4, sched.h, struct mm_struct): unsigned dumpable:1; What does this mean? It's a bitfield member. Your code means dumpable occupies exactly 1 bit in the structure. Bitfields are used when you want to pack members in bit-level. This can greatly reduce the size of memory used when there are a lot of flags in the structure. For example, if we define a

C++11 standard conformant bitmasks using enum class

橙三吉。 提交于 2019-12-02 17:40:13
Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <. Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the following does not compile with GCC (besides being extremely dangerous as it allows an erroneous

What is the most efficient way to represent small values in a struct?

主宰稳场 提交于 2019-12-02 17:27:06
Often I find myself having to represent a structure that consists of very small values. For example, Foo has 4 values, a, b, c, d that, range from 0 to 3 . Usually I don't care, but sometimes, those structures are used in a tight loop; their values are read a billion times/s, and that is the bottleneck of the program; the whole program consists of a big array of billions of Foo s; In that case, I find myself having trouble deciding how to represent Foo efficiently. I have basically 4 options: struct Foo { int a; int b; int c; int d; }; struct Foo { char a; char b; char c; char d; }; struct Foo

Colons after variable name in C [duplicate]

孤街醉人 提交于 2019-12-02 16:44:25
Possible Duplicate: What does a colon in a struct declaration mean, such as :1, :7, :16, or :32? This is C code sample of a reference page. signed int _exponent:8; What's the meaning of the colon before '8' and '8' itself? EboMike It's a bitfield. It's only valid in a struct definition, and it means that the system will only use 8 bits for your integer. It's a bitfield, an obscure and misguided feature of structures. That should be enough for you to lookup the information you need to know to deal with bitfields in other people's code. As for your own code, never use bitfields. Edit: As

Why is the enum incompatible with bit fields in Windows?

隐身守侯 提交于 2019-12-02 13:52:19
问题 I'm working on EBDS protocol interface for Windows and Linux. I'm trying to pack all the data required by the protocol into structs, then I write the struct itself and all the other stuff into the serial port sending it to the device. First part of the protocol is the data packaging, and one of the parts of the package is the Control Byte that matchs this description: Bit 0: Acknowledgement bit (switchs between 0 and 1 in each send). Bit 1 to 3: Device Type. Bit 4 to 6: Message Type. Bit 7:

Size of a structure having unsigned short ints

为君一笑 提交于 2019-12-02 13:42:23
I was surfing in one of our organisational data documents and I came across the following piece of code. struct A { unsigned short int i:1; unsigned short int j:1; unsigned short int k:14; }; int main(){ A aa; int n = sizeof(aa); cout << n; } Initially I thought the size will be 6 bytes as the size of the unsigned short int is 2 bytes. but the output of the above code was 2 bytes(On visual studio 2008). Is there a slight possibility that the i:1 , j:1 and k:14 makes it a bit field or something? Its just a guess and I am not very sure about it. Can somebody please help me in this? Yes, this is

Struct variable doesn't changed by assignment

守給你的承諾、 提交于 2019-12-02 13:22:44
struct st { int a1 : 3; int a2 : 2; int a3 : 1; } void main(void) { x.a3 = -1; if (x.a3 == -1) printf("TRUE\n"); else printf("FALSE\n"); x.a3 = 1; if (x.a3 == 1) printf("TRUE\n"); else printf("FALSE\n"); } In case, 'x.a3 = -1;' First if is TRUE . But, why 'x.a3 = 1' doesn't changed in second if ? It's still x.a3 = -1. And If I type 'x.a3 = 1;' in first if, it still x.a3 = = 1 !! It doesn't changed! Debug Result in XCode The problem is, a signed 1 bit variable can hold only two values, -1 and 0 (Read about Two's complement ). It is not sufficient to hold a value of 1 (+ 1 , to be exact). To

Why is the enum incompatible with bit fields in Windows?

孤街浪徒 提交于 2019-12-02 04:08:52
I'm working on EBDS protocol interface for Windows and Linux. I'm trying to pack all the data required by the protocol into structs, then I write the struct itself and all the other stuff into the serial port sending it to the device. First part of the protocol is the data packaging, and one of the parts of the package is the Control Byte that matchs this description: Bit 0: Acknowledgement bit (switchs between 0 and 1 in each send). Bit 1 to 3: Device Type. Bit 4 to 6: Message Type. Bit 7: Unused. To handle tis control byte i created two enums and one struct: enum E_DEVICE_TYPE { E_BILL