How bit field store in memory?.

Thread Starter

tej1200

Joined Jun 26, 2013
5
How bit field store in memory?.Is store left to right or right to left?
example:
struct stud
{
unsigned int x:5;
unsigned int y:5;
unsigned int z:6;
};
struct stud bit={1,3,3};

How o/p of below program is 12.

#include<stdio.h>
#include<conio.h>
void main
{
struct stud
{
unsigned int x:5;
unsigned int y:5;
unsigned int z:6;
};
struct stud bit={1,3,3};
char *p;
clrscr();
p=&bit;
p++;
printf("%d",*p);

}
 

ErnieM

Joined Apr 24, 2011
8,377
How bit field store in memory?.Is store left to right or right to left?
It may be stored either way. You could write a test program to explore how your particular compiler does it. Chances are if you keep to that compiler your code will not break over the following compiler revisions.
 

t06afre

Joined May 11, 2009
5,934
According to "The C Programming Language" by Kernighan & Ritchie say this about the bit fields
Almost everything about fields is implementation-dependent. Whether a field may overlap a word boundary is implementation-defined. Fields need not be names; unnamed fields (a colon and width only) are used for padding. The special width 0 may be used to force alignment at the next word boundary.
Fields are assigned left to right on some machines and right to left on others. This means that although fields are useful for maintaining internally-defined data structures, the question of which end comes first has to be carefully considered when picking apart externally-defined data; programs that depend on such things are not portable. Fields may be declared only as ints; for portability, specify signed or unsigned explicitly. They are not arrays and they do not have addresses, so the & operator cannot be applied on them.
 
Top