M
Marcel Müller
Hi,
is the following code valid?
#include <stdio.h>
struct X
{ bool f1:1;
bool f2:1;
};
int main(int argc, char* argv[])
{ X x;
x.f1 = true;
x.f2 = false;
printf("%u %u %u\n", x.f1, x.f2, sizeof x);
return 0;
}
While all my compiler seem to eat the combination of bool and bit
fields, it is not that straight forward, because normally it must be an
unsigned integer type. And there is no implicit conversion from that to
bool.
In fact, the compilers also translate the following structure without
warning:
struct X
{ bool f1:2;
bool f2:2;
};
If the code at the top is valid I would like to prefer this over
enumeration types with the usual power of two values.
Unfortunately at least gcc seems not to optimize expressions like
if (x.f2)
in a way to avoid the logical shift.
Marcel
is the following code valid?
#include <stdio.h>
struct X
{ bool f1:1;
bool f2:1;
};
int main(int argc, char* argv[])
{ X x;
x.f1 = true;
x.f2 = false;
printf("%u %u %u\n", x.f1, x.f2, sizeof x);
return 0;
}
While all my compiler seem to eat the combination of bool and bit
fields, it is not that straight forward, because normally it must be an
unsigned integer type. And there is no implicit conversion from that to
bool.
In fact, the compilers also translate the following structure without
warning:
struct X
{ bool f1:2;
bool f2:2;
};
If the code at the top is valid I would like to prefer this over
enumeration types with the usual power of two values.
Unfortunately at least gcc seems not to optimize expressions like
if (x.f2)
in a way to avoid the logical shift.
Marcel