Hi everybody...
The following code says that the bit field has a size of 4 bytes:
Code:
typedef struct BitField{
unsigned int A:1;
} BitField;
int main(int argc, char *argv[]){
printf("%d\n", sizeof(BitField));
}
And the following code says that the bit field has a size of 2 bytes (It also gives some sort of warning related to the fact that I'm using unsigned short to create a bit field):
Code:
typedef struct BitField{
unsigned short int A:1;
} BitField;
int main(int argc, char *argv[]){
printf("%d\n", sizeof(BitField));
}
Finally, the following code also says that the bit field has a size of 2 bytes:
Code:
typedef struct BitField{
unsigned short int A;
} BitField;
int main(int argc, char *argv[]){
printf("%d\n", sizeof(BitField));
}
So, given that I pretend to create a bit field of three int's (each with 2 bits only), using the less amount of memory possible, what is the best way to create the bit field, " unsigned int A:2; " or " unsigned short A; "?
I'm aware that the computer can't access blocks of memory with 2 bits... but why a 1-bit unsigned int occupies more memory than an unsigned short (which has 2 bytes)?
Thank You...