Structure fields

A structure can have members that are as small as a single bit, called a bit-field. In this case, you declare an integer member with the number of bits that the member will take up. You are able to declare unnamed members. For example, you may have a structure that holds information about the length of an item, and whether the item has been changed (is dirty). The item this refers to has a maximum size of 1,023, so you need an integer with at least 10 bits of width to hold this. You could use an unsigned short to hold both the length and the dirty information:

    void print_item_data(unsigned short item)     {         unsigned short size = (item & 0x3ff);         char *dirty = (item > 0x7fff) ? "yes" : "no";   cout << "length " << size << ", ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.