What Does a Colon in a Struct Declaration Mean, Such as :1, :7, :16, or :32

What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?

The 1 and the 7 are bit sizes to limit the range of the values. They're typically found in structures and unions. For example, on some systems (depends on char width and packing rules, etc), the code:

typedef struct {
unsigned char a : 1;
unsigned char b : 7;
} tOneAndSevenBits;

creates an 8-bit value, one bit for a and 7 bits for b.

Typically used in C to access "compressed" values such as a 4-bit nybble which might be contained in the top half of an 8-bit char:

typedef struct {
unsigned char leftFour : 4;
unsigned char rightFour : 4;
} tTwoNybbles;

For the language lawyers amongst us, the 9.6 section of the C++11 standard explains this in detail, slightly paraphrased:


Bit-fields [class.bit]

A member-declarator of the form

     identifieropt   attribute-specifieropt   :   constant-expression

specifies a bit-field; its length is set off from the bit-field name by a colon. The optional attribute-specifier appertains to the entity being declared. The bit-field attribute is not part of the type of the class member.

The constant-expression shall be an integral constant expression with a value greater than or equal to zero. The value of the integral constant expression may be larger than the number of bits in the object representation of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation of the bit-field.

Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit.

Note: bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. - end note

C struct: what does this mean? [duplicate]

These are bit fields: https://en.wikipedia.org/wiki/Bit_field. Here you just reserve 1 bit for 'flanke' and one for the 'lastState'. The type has to be unsigned int.

What is the meaning of colon (:) operator in uint isWidget : 1; in Qt? [duplicate]

This is part of C struct notation - you can specify the size of an integer field in bits by using a : numBits after the property name.

I must assume that the same syntax can be used in a C++ class (i'm a C guy, but i'm sure that this is doing the same thing in C++)

Colon after class member declaration? [duplicate]

This syntax is bitfields.

struct Test {
size_t a : 2; // Occupies two bits
size_t b : 3; // Occupies three bits
size_t c : 4; // Occupies four bits
};

: (colon) in C struct - what does it mean? [duplicate]

Those are bit fields. Basically, the number after the colon describes how many bits that field uses. Here is a quote from MSDN describing bit fields:

The constant-expression specifies the width of the field in bits. The
type-specifier for the declarator must be unsigned int, signed int, or
int, and the constant-expression must be a nonnegative integer value.
If the value is zero, the declaration has no declarator. Arrays of bit
fields, pointers to bit fields, and functions returning bit fields are
not allowed. The optional declarator names the bit field. Bit fields
can only be declared as part of a structure. The address-of operator
(&) cannot be applied to bit-field components.

Unnamed bit fields cannot be referenced, and their contents at run
time are unpredictable. They can be used as "dummy" fields, for
alignment purposes. An unnamed bit field whose width is specified as 0
guarantees that storage for the member following it in the
struct-declaration-list begins on an int boundary.

This example defines a two-dimensional array of structures named screen.

struct 
{
unsigned short icon : 8;
unsigned short color : 4;
unsigned short underline : 1;
unsigned short blink : 1;
} screen[25][80];

Edit: another important bit from the MSDN link:

Bit fields have the same semantics as the integer type. This means a
bit field is used in expressions in exactly the same way as a variable
of the same base type would be used, regardless of how many bits are
in the bit field.

A quick sample illustrates this nicely. Interestingly, with mixed types the compiler seems to default to sizeof (int).

  struct
{
int a : 4;
int b : 13;
int c : 1;
} test1;

struct
{
short a : 4;
short b : 3;
} test2;

struct
{
char a : 4;
char b : 3;
} test3;

struct
{
char a : 4;
short b : 3;
} test4;

printf("test1: %d\ntest2: %d\ntest3: %d\ntest4: %d\n", sizeof(test1), sizeof(test2), sizeof(test3), sizeof(test4));

test1: 4

test2: 2

test3: 1

test4: 4

struct definition with ':' after name of the variable [duplicate]

It specifies the number of bits to use for that field. It is called "bit field".

Check this: https://en.cppreference.com/w/cpp/language/bit_field

In C, what does a colon mean inside a declaration? [duplicate]

It's a bitfield member. Your code means dumpable occupies exactly 1 bit in the structure.

Bitfields are used when you want to pack members in bit-level. This can greatly reduce the size of memory used when there are a lot of flags in the structure. For example, if we define a struct having 4 members with known numeric constraint

0 < a < 20
b in [0, 1]
0 < c < 8
0 < d < 100

then the struct could be declared as

struct Foo {
unsigned a : 5; // 20 < 2^5 = 32
unsigned b : 1; //
unsigned c : 3; //
unsigned d : 7; // 100 < 2^7 = 128
};

then the bits of Foo may be arranged like

                      ddddddd c  cc b aaaaa
--------- --------- --------- ----------
octet 1 octet 0
===========================================
uint32

instead of

struct Foo {
unsigned a;
unsigned b;
unsigned c;
unsigned d;
};

in which many bits are wasted because of the range of values

# wasted space which is not used by the program
# v v
ddddddd ccc
------------------------------------ ------------------------------------
uint32 uint32


b aaaaa
------------------------------------ ------------------------------------
uint32 uint32

so you can save space by packing many members together.

Note that the C standard doesn't specify how the bitfields are arranged or packed within an "addressable storage unit". Also, bitfields are slower compared with direct member access.

What does this construction means? [duplicate]

These are called "bit fields". has_some_value1 occupies one bit. has_some_value2 also occupies one bit—maybe the next physical bit in memory, or maybe not (depends how your compiler is configured to handle bit field alignment).



Related Topics



Leave a reply



Submit