C- Size of structure when bit field is used and how it is stored in memory

后端 未结 3 988
后悔当初
后悔当初 2021-01-06 04:13
void main()
{
  struct bitfield
  {
    signed int a :3;
    unsigned int b :13;
    unsigned int c :1;
  };

  struct bitfield bit1 = { 2, 14, 1 };
  clrscr();
  pr         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-06 04:21

    how the bit field is stored is architeture dependent means for little endian left to right and for big enian right to left. the storage in the memory happens with the word size.1 byte is 1 word.so in our example its 17 bit as first two 8 bits are going to be stored in first two word.the next bit will be stored in the next word .the size should be 3 byte but the compiler does some padding so finally the size is becoming 4 byte.

    for the storage in the memory you can refer this link http://en.wikipedia.org/wiki/Endianness

提交回复
热议问题