Jacek Dziedzic <ja************ *@janowo.net> wrote :
Ron Natalie wrote: "Philipp" <no************ *@hotmail.com> wrote:
Hello
I'm working on a piece of code which I did not write and it has
variables defined as:
unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
Look up "bitfield" (or perhaps "bit field") in your C text.
Actually you need to look up "bit-field", if searching the Standard
Whoa, what are these? I've never seen a thing like that before! :).
Is this Standard C++, or C perhaps? Or a relic of some sorts like
the auto keyword? My compiler gives a "declaratio n syntax error" for:
// ---
int main() {
unsigned short k:13;
}
They've been in C for decades but are not widely used. They are only
valid within structs or unions, which is why your code is an error.
Also, bit-fields must be _Bool, int, unsigned int, or signed int,
although it's a common extension to allow "unsigned short" etc.
In the original example, if unsigned short has at least 29 bits,
then there will only be 1 actual unsigned short in the struct,
and "i", "j" and "k" will be packed into it, and whenever you use
i,j,k in expressions, the compiler automatically twiddles the bits.
Otherwise they would be packed into 2 unsigned shorts.
Probably why they are unpopular is that you can't do 'sizeof' on them
and you can't take their address (hence, you can't pass pointers to
them to a function, for example), and the packing method is
implementation-defined. I don't think anyone would really mind if
they were deprecated.