Search code examples
cembedded

Defining a bit, but without a name?


So I have some code for the iodefine of my board. I see a lot of these in structs. What exactly is it doing? Is it just a placeholder for the last 4 bits? Why doesn't it cause a compiler error and what is it used for?

union {
    unsigned char BYTE;
    struct {
        unsigned char OVRF:1;
        unsigned char IDLNF:1;
        unsigned char MODF:1;
        unsigned char PERF:1;
        unsigned char :4;        <------------
    } BIT;
} SPSR;

I hope thats not too many questions, I just found this very interesting.


Solution

  • It's an unnamed bit-field. It is used to provide padding (usually between adjacent bit-fields).

    (C99, 6.7.2.1p11) "A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field"