bit-fields

Signed bit field represetation

。_饼干妹妹 提交于 2019-12-17 20:58:27
问题 I made a bit field with a field sized 1 bit, and used int instead of unsigned . Later on when i tried to check the value of the field i found that the value was -1. I used this code to check the binary represantation and the value of my bit field: #include <stdio.h> #include <stdlib.h> union { struct { int bit:1; } field; int rep; } n; int main() { int c, k; n.field.bit=1; for (c = 31; c >= 0; c--) { k = n.rep >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n %d \n", n.field.bit);

Why aren't bitfields allowed with normal variables?

百般思念 提交于 2019-12-17 19:26:25
问题 I wonder why bitfields work with unions/structs but not with a normal variable like int or short . This works: struct foo { int bar : 10; }; But this fails: int bar : 10; // "Expected ';' at end of declaration" Why is this feature only available in unions/structs and not with variables? Isn't it technical the same? Edit: If it would be allowed you could make a variable with 3 bytes for instance without using the struct/union member each time. This is how I would to it with a struct: struct

How to simulate bit-fields in Delphi records?

自古美人都是妖i 提交于 2019-12-17 15:29:12
问题 I would like to declare a record in Delphi that contains the same layout as it has in C. For those interested : This record is part of a union in the Windows OS's LDT_ENTRY record. (I need to use this record in Delphi because I'm working on an Xbox emulator in Delphi - see project Dxbx on sourceforge). Anyway, the record in question is defined as: struct { DWORD BaseMid : 8; DWORD Type : 5; DWORD Dpl : 2; DWORD Pres : 1; DWORD LimitHi : 4; DWORD Sys : 1; DWORD Reserved_0 : 1; DWORD Default

What is VC++ doing when packing bitfields?

梦想与她 提交于 2019-12-17 14:03:26
问题 To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 6; unsigned int j : 6; unsigned int k : 4; unsigned int l : 15; }; #pragma pack(pop) struct cc c; int main(int argc, char **argv) { printf("%d\n",sizeof(c)); } The output is "8", meaning that the 56

What is VC++ doing when packing bitfields?

ε祈祈猫儿з 提交于 2019-12-17 14:03:15
问题 To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 6; unsigned int j : 6; unsigned int k : 4; unsigned int l : 15; }; #pragma pack(pop) struct cc c; int main(int argc, char **argv) { printf("%d\n",sizeof(c)); } The output is "8", meaning that the 56

Questions about C bitfields

天大地大妈咪最大 提交于 2019-12-17 13:55:05
问题 Is bitfield a C concept or C++? Can it be used only within a structure? What are the other places we can use them? AFAIK, bitfields are special structure variables that occupy the memory only for specified no. of bits. It is useful in saving memory and nothing else. Am I correct? I coded a small program to understand the usage of bitfields - But, I think it is not working as expected. I expect the size of the below structure to be 1+4+2 = 7 bytes (considering the size of unsigned int is 4

How is the size of a struct with Bit Fields determined/measured?

醉酒当歌 提交于 2019-12-17 08:25:38
问题 #include <stdio.h> typedef struct size { unsigned int a:1; unsigned int b:31; unsigned int c:1; } mystruct; int main() { mystruct a; printf("%d", sizeof(a)); return 0; } With int b:31 , the output is 8. With int b:1 , the output is 4. With int b:32 , the output is 12. Can somebody explain the reason for this? 回答1: You don't say whether you know what bitfields are, but I'll assume you do. On your implementation, evidently unsigned int is a 32 bit integer, occupying 4 bytes. This accounts for

How is the size of a struct with Bit Fields determined/measured?

社会主义新天地 提交于 2019-12-17 08:25:11
问题 #include <stdio.h> typedef struct size { unsigned int a:1; unsigned int b:31; unsigned int c:1; } mystruct; int main() { mystruct a; printf("%d", sizeof(a)); return 0; } With int b:31 , the output is 8. With int b:1 , the output is 4. With int b:32 , the output is 12. Can somebody explain the reason for this? 回答1: You don't say whether you know what bitfields are, but I'll assume you do. On your implementation, evidently unsigned int is a 32 bit integer, occupying 4 bytes. This accounts for

Are there reasons to avoid bit-field structure members?

可紊 提交于 2019-12-14 03:44:48
问题 I long knew there are bit-fields in C and occasionally I use them for defining densely packed structs: typedef struct Message_s { unsigned int flag : 1; unsigned int channel : 4; unsigned int signal : 11; } Message; When I read open source code, I instead often find bit-masks and bit-shifting operations to store and retrieve such information in hand-rolled bit-fields. This is so common that I do not think the authors were not aware of the bit-field syntax, so I wonder if there are reasons to

Bitfields and alignment

99封情书 提交于 2019-12-14 02:50:30
问题 Trying to pack data into a packet. This packet should be 64 bits. I have this: typedef union { uint64_t raw; struct { unsigned int magic : 8; unsigned int parity : 1; unsigned int stype : 8; unsigned int sid : 8; unsigned int mlength : 31; unsigned int message : 8; } spacket; } packet_t; But it seems that alignment is not guaranteed. Because when I run this: #include <strings.h> #include <stdio.h> #include <stddef.h> #include <stdint.h> const char *number_to_binary(uint64_t x) { static char b