0

I am new to C coding and I ran into ":" in the following structure

struct __tag131
{                                                          /* Bit Access       */
unsigned char  P0P6:1;                                 /* P0P6             */
unsigned char  P1P6:1;                                 /* P1P6             */
unsigned char  P2P6:1;                                 /* P2P6             */
unsigned char  P3P6:1;                                 /* P3P6             */
unsigned char  :2;                                     /* Reserved Bits    */
unsigned char  TBP6:1;                                 /* TBP6             */
unsigned char  MKP6:1;                                 /* MKP6             */
unsigned char  :4;                                     /* Reserved Bits    */
unsigned char  RFP6:1;                                 /* RFP6             */
unsigned char  :2;                                     /* Reserved Bits    */
unsigned char  CTP6:1;                                 /* CTP6             */
}; 

What is the use of ":" ? Is it even a operator ? I know what a unsigned char 'type' is but what is unsigned char :2 ? Please explain.

Rookie_Coder2318
  • 81
  • 1
  • 1
  • 7

2 Answers2

2

No, the ":" are bitfields. You can read about them more at:

Idan
  • 333
  • 2
  • 8
0

A bit field in C allows a compressed form of information. The number after the colon describes the number of bits which are allowed for storage.

1 bit => 0,1   (or -1,0 )
2 bits => 0,3  (or -2,1 )
3 bits => 0,7  (or -4,3 )

The data is slower to access, but uses less space.

mksteve
  • 12,614
  • 3
  • 28
  • 50