1

Apologies if this is a basic question but I'm seeing these colons (:) for the first time in the definition of the struct. What do these colons do?

struct gsm48_meas_res {
    uint8_t rxlev_full:6,
         dtx_used:1,
         ba_used:1;
    uint8_t rxlev_sub:6,
         meas_valid:1,
         spare:1;
    uint8_t no_nc_n_hi:1,
         rxqual_sub:3,
         rxqual_full:3,
         spare2:1;
    uint8_t rxlev_nc1:6,
         no_nc_n_lo:2;
    uint8_t bsic_nc1_hi:3,
         bcch_f_nc1:5;
    uint8_t rxlev_nc2_hi:5,
         bsic_nc1_lo:3;
    uint8_t bsic_nc2_hi:2,
         bcch_f_nc2:5,
         rxlev_nc2_lo:1;
    uint8_t rxlev_nc3_hi:4,
         bsic_nc2_lo:4;
    uint8_t bsic_nc3_hi:1,
         bcch_f_nc3:5,
         rxlev_nc3_lo:2;
    uint8_t rxlev_nc4_hi:3,
         bsic_nc3_lo:5;
    uint8_t bcch_f_nc4:5,
         rxlev_nc4_lo:3;
    uint8_t rxlev_nc5_hi:2,
         bsic_nc4:6;
    uint8_t bcch_f_nc5_hi:4,
         rxlev_nc5_lo:4;
    uint8_t rxlev_nc6_hi:1,
         bsic_nc5:6,
         bcch_f_nc5_lo:1;
    uint8_t bcch_f_nc6_hi:3,
         rxlev_nc6_lo:5;
    uint8_t bsic_nc6:6,
         bcch_f_nc6_lo:2;
} __attribute__ ((packed));

I am facing some problems in assigning values to these uint8_t variables. Initially I thought it is the default assignment but after exhausting all other possibilities, I think it has something to do with these colons.

user5104026
  • 678
  • 1
  • 6
  • 22

1 Answers1

3

It is a bit-field, which is something very poorly regulated by any standard. For example

uint8_t rxlev_full:6,
         dtx_used:1,
         ba_used:1;

means "somewhere in the memory, give me 6 bits then 1 bit then 1 bit". You cannot know or assume the bit order portably - it is compiler-specific. Also, standard bit-fields are only defined for _Bool, signed int and unsigned int. Using uint8_t is a non-standard extension.

So there is no telling what this code will actually do. You have to read your compiler manual to know the specifics.

Lundin
  • 195,001
  • 40
  • 254
  • 396