Hello. If anyone could help me understand how to use these bit-fields, that would be great.
Both of my C texts tell me that I can instantiate a struct like this:
The console tells me that this struct is four bytes long. Shouldn't it be only one byte?
If I try this:
The console tells me that the struct is only one byte, but both of my C texts tell me not to use chars for bit-fields.
If anyone can explain what's going on, that would be great.
Both of my C texts tell me that I can instantiate a struct like this:
Code:
#include <stdio.h>
int main (int argc, char *argv[])
{
struct intStruct
{
unsigned int value0 : 1;
unsigned int value1 : 1;
unsigned int value2 : 1;
unsigned int value3 : 1;
unsigned int value4 : 1;
unsigned int value5 : 1;
unsigned int value6 : 1;
unsigned int value7 : 1;
};
printf("%lu\n", sizeof(struct intStruct));
return 0;
}
The console tells me that this struct is four bytes long. Shouldn't it be only one byte?
If I try this:
Code:
#include <stdio.h>
int main (int argc, char *argv[])
{
struct charStruct
{
unsigned char value0 : 1;
unsigned char value1 : 1;
unsigned char value2 : 1;
unsigned char value3 : 1;
unsigned char value4 : 1;
unsigned char value5 : 1;
unsigned char value6 : 1;
unsigned char value7 : 1;
};
printf("%lu\n", sizeof(struct charStruct));
return 0;
}
The console tells me that the struct is only one byte, but both of my C texts tell me not to use chars for bit-fields.
If anyone can explain what's going on, that would be great.