0

I saw this being used, it is in a structure. Only time I have seen ":" being used is for conditionals. But in this case it is part of uint64_t variable declaration.

typedef struct
{
   uint64_t             attrOne:1;
   uint64_t             attrTwo:1;
   uint64_t             attrThree:1;
   uint64_t             attrFour:1;
   uint64_t             attrFive:1;
   uint64_t             attrSix:1;
   uint64_t             attrSeven:1;
   uint64_t             reserved0:55;
   uint64_t             reserved1;
} StructFlagValues;

Not too sure what the ":" operator in this case means exactly. Is it related to bit positions?

Thanks.

  • Those are bit fields. The number after ":" tells how many bits the field occupies. – Hristo Iliev May 08 '15 at 20:46
  • 2
    Do not think of it in terms of "positions" however. Be aware that the exact representation is up to the compiler. You could not rely on the bits being in big-endian or little-endian order, for example. – David Hoelzer May 08 '15 at 20:49

2 Answers2

1

It means that each of first seven fields take 1 bit each, followed by 55 bit long integer.

Read more here: http://www.tutorialspoint.com/cprogramming/c_bit_fields.htm

Ishamael
  • 12,583
  • 4
  • 34
  • 52
1

The : operator can detonate bit fields.

This means that each "field" is mapped to some of the bits of a specific byte.

In your sample, it means that each field is one bit long (either 0 or 1), and the compiler applies all logic to make sure you access the specific field only.

Mark Segal
  • 5,427
  • 4
  • 31
  • 69
  • 1
    I assume you meant "denote" rather than "detonate". The `:` does not cause explosions. – Peter May 08 '15 at 23:09