I want to have a typedef that is 1-bit integer, so I though of this typedef int:1 FLAG; but I'm getting errors with it, is there a way I can do so?
Thanks
No.
The smallest addressable "thing" in a C Program is a byte or char.
A char is at least 8 bits long.
So you cannot have a type (or objects of any type) with less than 8 bits.
What you can do is have a type for which objects occupy at least as many bits as a char and ignore most of the bits
#include <limits.h>
#include <stdio.h>
struct OneBit {
unsigned int value:1;
};
typedef struct OneBit onebit;
int main(void) {
onebit x;
x.value = 1;
x.value++;
printf("1 incremented is %u\n", x.value);
printf("each object of type 'onebit' needs %d bytes (%d bits)\n",
(int)sizeof x, CHAR_BIT * (int)sizeof x);
return 0;
}
You can see the code above running at ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With