473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

failing to achieve desired size of a BitField structure

hello everyone,
i read somewhere that it is possible to define a BitFields structure
so i set out to:

Expand|Select|Wrap|Line Numbers
  1. struct IC_X
  2. {
  3. unsigned short int dump:2;
  4. unsigned short int ic1:10;
  5. unsigned short int ic2:10;
  6. unsigned short int ic3:10;
  7. };
  8. struct EVS
  9. {
  10. unsigned short int dump:2;
  11. unsigned short int v:10;
  12. unsigned short int p:10;
  13. unsigned short int q:10;
  14. };
  15. struct SE_P
  16. {
  17. unsigned short int dump:1;
  18. unsigned short int c1s:1;
  19. unsigned short int c2s:1;
  20. unsigned short int c3s:1;
  21. unsigned short int c1e:1;
  22. unsigned short int c2e:1;
  23. unsigned short int c3e:1;
  24. unsigned short int rC:1;
  25. };
  26. struct ALS
  27. {
  28. unsigned short int dump:5;
  29. unsigned short int al1:1;
  30. unsigned short int al3:1;
  31. unsigned short int al4:1;
  32. };
then
Expand|Select|Wrap|Line Numbers
  1. struct LogEntry
  2. {
  3. IC_X icx;
  4. EVS evs;
  5. SE_P sep;
  6. ALS als;
  7. FileTime TimeStamp;
  8. };
FileTime is just a typedef for an unsigned long.
dump fields are NOT used.

so basically i wanted to pack 3, 10-bit numbers in IC_X (and leave the
2 MSB bits unused), then another 3 10-bit nums in EVS, then 7 bools in
SE_P, and another 3 bools in ALS.
finally pack them all together (along with an unsigned long
timeStamp) to a 14byte long structure.
with this structure though i get a sizeof(LogEntry)=20 in execution...

what am i missing?
thank you for your help
nass

Feb 13 '07 #1
3 1511
On 13 Feb, 10:43, "nass" <athanasios.si...@gmail.comwrote:
<snip>

You work on a 32 bit machine.

It is all due to alignment of primitive types.
In IC_X, the firs two members fit in one short,
next two can only fit in two shorts, and since
short alignment is 2, sizeof(IC_X) == 6.

EVS is the same.

SEP members fit in one short, hence
sizeof(SEP) == 2. same with ALS.

FileTime is long which is aligned on
word boundary hence sizeof(FileTime) = 4

It all adds up to 20.

make IC_X and EVS to use unsigned
int instead of short and SEP and ALS
to use char and then look-up struct alignment
directive for your compiler and set it to 1
(remember to reset it afterwards)

Feb 13 '07 #2
thank you for the reply. in the end actually re-did the structure in
the following way:

struct LogEntry
{
unsigned ic1:10;
unsigned ic2:10;
unsigned ic3:10;
unsigned dm1:2;

unsigned v:10;
unsigned p:10;
unsigned q:10;
unsigned dm2:2;

unsigned c1s:1;
unsigned c2s:1;
unsigned c3s:1;
unsigned c1e:1;
unsigned c2e:1;
unsigned c3e:1;
unsigned rC:1;
unsigned dm3:1;

unsigned al1:1;
unsigned al3:1;
unsigned al4:1;
unsigned dm4:5;

unsigned long TimeStamp;
} __attribute__ ((__packed__));

now sizeof(logEntry) returns 14 as expected. the 'dm*' are dummy
unused variables.
Thank you for your help
nass

Feb 13 '07 #3
nass wrote:
thank you for the reply. in the end actually re-did the structure in
the following way:

struct LogEntry
{
unsigned ic1:10;
unsigned ic2:10;
unsigned ic3:10;
unsigned dm1:2;

unsigned v:10;
unsigned p:10;
unsigned q:10;
unsigned dm2:2;

unsigned c1s:1;
unsigned c2s:1;
unsigned c3s:1;
unsigned c1e:1;
unsigned c2e:1;
unsigned c3e:1;
unsigned rC:1;
unsigned dm3:1;

unsigned al1:1;
unsigned al3:1;
unsigned al4:1;
unsigned dm4:5;

unsigned long TimeStamp;
} __attribute__ ((__packed__));

now sizeof(logEntry) returns 14 as expected. the 'dm*' are dummy
unused variables.
A more portable way of doing that is to specify a width of 0, which
tells the compiler to advance to the next allocation unit boundary.
Also, you don't need to give names to the ones that are used only for
padding. Of course, none of the details are guaranteed: you have to
adapt to the compiler you're using.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: User | last post by:
Anyone have ideas which os command could be used to get the size of a file without actually opening it? My intention is to write a script that identifies duplicate files with different names. I...
9
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne:...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
3
by: Andy Venikov | last post by:
Sometimes you want to use a bitfield to hold an enum value. In such cases you would only use as many bits as are needed to encode the full set of enum values. And it is a pain to recompute and...
11
by: Ken Allen | last post by:
I have been using C# for some time now, and for the most part I am very impressed, and prefer using it over C++. There is one area where C#/.Net seem, at least to me, to be less than desirable...
11
by: chinu | last post by:
mail hi all.. is it possible to find the size of a structure upto bit size.., where the structure contains only one element and that too with bit specifier..eg.. struct x { char a : 4; } y;
6
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
7
by: arne | last post by:
Hi all, cleaning up some elderly code, I stumbled across the following: /**************************************************/ struct { uint bf:8; char a1; char a2;
6
by: shaun roe | last post by:
For a bit of seasonal festive fun, I thought I'd try making a bitfield function, i.e. a function returning, for example, the value of bits 1 to 5 of a word as an integer, when the exact bits are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.