1

I am dealing with Ternary Search Tree and I encountered a different thing in ternary search tree node structure:

struct Node {
    char data;

    // True if this character is last character of one of the words
    unsigned isEndOfString: 1;

    struct Node *left, *eq, *right;
};

It is compliled with both C++11 and older versions. I wonder what this unsigned isEndOfString: 1 means? How it is different from bool isEndOfString = true ? What type this statement refers actually and when it is convenient to use such syntax?

Kaidul
  • 15,409
  • 15
  • 81
  • 150

1 Answers1

2

It is a bit field of size 1. So isEndOfString is a single bit whose value is either 0 or 1.

Basically it allows you here to store a boolean flag in a single bit, minimizing the memory footprint of Node.

Note that bitfields are inherited from C.

quantdev
  • 23,517
  • 5
  • 55
  • 88