Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitfield querry .... 2

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
0
0
IN
Hi All,

I am trying to understand the concept behind bitmap. I need to know how many bytes the structure containing bitfield occupy.

For example,

struct {
unsigned int A : 2;
unsigned int AA : 2;
unsigned int AAA : 2;
}mystruct;

here in the above case, what will be the sizeof mystruct ??? If i change the data type of the member , say AAA , to double then how it will affest the size. Or suppose i say unsigned int AAA : 32 then what will be the effect.

I did not get the memory picture in this case. Please help.

Thanks in advance.
Sanjay
 
> here in the above case, what will be the sizeof mystruct ???
No idea - write some code to print it and find out
Code:
printf( "size=%ld\n", sizeof(struct mystruct) );
For something containing only 6 bits, the answer should be just one byte.

> say AAA , to double then how it will affest the size.
You can't - bitfields can only be int or unsigned int

> Or suppose i say unsigned int AAA : 32 then what will be the effect.
Again try it - the answer varies from machine to machine
Typically, if a bit-field cannot fit within the current storage unit, the compiler leaves a few unused bits and uses the next complete storage unit.
 
Your structure is 1 byte. It should really be defined as

struct {
unsigned int A : 2;
unsigned int AA : 2;
unsigned int AAA : 2;
unsigned; /* for the remaining bits */
}mystruct;

The basic formula is ((total bits - 1)/8)+1. In this case ((6 - 1)/8+1 i.e. 1 byte. If you set AAA to 32 bits then it would be ((36 - 1)/8 + 1 which is 5 bytes. Having said that, you have to consider alignment. Some compilers align on 8 byte boundaries so anything that is 1 to 8 bytes will take up 8 bytes. Also, there is a limit on the number of bits you can specify. Normally this is the word size of your machine. For instance, you couldn't specify AAA:128 on a 16 bit machine. It may compile but it probably wouldn't work.


 
Also, to deal with alignment issues, the compiler is free to insert "padding bytes" between members of structs.

There is no definitive answer to your question. It depends on the machine, as the others said.
 
Like others have said before, all the fields declared in the structure are implementation dependent. They generally are aligned in a word boundary.padding is done to ensure this. this can be done by just specifying a colon followed by a width. The width of 0 is given to force to align at the next word boundary.
The fields in the structure are allocated left to right or right to left on some.
Another 1 point worth noting is that , they do not have addresses, as in the "& operator" cant be applied to them.
 
Can you point out the part of the C standard that specifies that a bit field width of 0 forces alignment as you say? I wasn't aware that 0 was even valid for a bit field width, much less that it could be used to force alignment.
 
In the draft copy of C99, the relevant paragraph is 6.7.2.1
Also K&R 2nd Ed mentions it on page 213
 
dlkfjdlfkd,
I'd found this piece of info from K & R .. Check out chapter 6, (structures), Page 150... Second Edition
thnx
-vs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top