12

I found this line here:

uint32 bIsHungry : 1;

... and I've never seen this syntax used to initialize a variable.

I'm used to seeing this:

uint32 bIsHungry = 1;

It looks kind of like an initialization list, but for a single field?

What is it, what does it do, why should I care?

Marco A.
  • 43,032
  • 26
  • 132
  • 246
Cody Smith
  • 2,732
  • 3
  • 31
  • 43

3 Answers3

15

That line is a bit field declaration and it declares a data member with an explicit bit-level size

Example from cppreference:

#include <iostream>
struct S {
 // three-bit unsigned field,
 // allowed values are 0...7
 unsigned int b : 3;
};
int main()
{
    S s = {7};
    ++s.b; // unsigned overflow
    std::cout << s.b << '\n'; // output: 0
}

Notice that in the example above the unsigned overflow is defined behavior (same doesn't apply if b were declared as a signed type)

The documentation you links also states that

Boolean types can be represented either with the C++ bool keyword or as a bitfield

Regarding why should I care I recommend reading this other question

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
2

This is bitfield declaration, Declares a class data member with explicit size, in bits.

Nishant
  • 1,635
  • 1
  • 11
  • 24
2

It is not initialization, it is a fragment of a declaration.

struct {
    // ...
    uint32 bIsHungry : 1;
    // ...
};

declares a bIsHungry as a bitfield member of the struct. It says that bIsHungry is an unsigned int whose length is 1 bit. Its possible values are 0 and 1.

Read more about bitfields: http://en.cppreference.com/w/c/language/bit_field

axiac
  • 68,258
  • 9
  • 99
  • 134