What Does 'Unsigned Temp:3' in a Struct or Union Mean

What does 'unsigned temp:3' in a struct or union mean?

This construct specifies the length in bits for each field.

The advantage of this is that you can control the sizeof(op), if you're careful. the size of the structure will be the sum of the sizes of the fields inside.

In your case, size of op is 32 bits (that is, sizeof(op) is 4).

The size always gets rounded up to the next multiple of 8 for every group of unsigned xxx:yy; construct.

That means:

struct A
{
unsigned a: 4; // 4 bits
unsigned b: 4; // +4 bits, same group, (4+4 is rounded to 8 bits)
unsigned char c; // +8 bits
};
// sizeof(A) = 2 (16 bits)

struct B
{
unsigned a: 4; // 4 bits
unsigned b: 1; // +1 bit, same group, (4+1 is rounded to 8 bits)
unsigned char c; // +8 bits
unsigned d: 7; // + 7 bits
};
// sizeof(B) = 3 (4+1 rounded to 8 + 8 + 7 = 23, rounded to 24)

I'm not sure I remember this correctly, but I think I got it right.

unsigned data types in structures

The default type assumed here is unsigned int, this is assumed by the compiler when you specify just unsigned.

The bitfield syntax unsigned vleft : 1 specifies the width in bits of the data field, in this situation it means that it's a single bit flag (which can be either 0 or 1). This is used to pack many fields of the structure in less bits (when you don't need to waste, like in this case, a whole char or int for just storing a flag).

C struct: what does this mean?

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 meaning of : in struct C

That is a bit field.

It basically tells the compiler that hey, this variable only needs to be x bits wide, so pack the rest of the fields in accordingly, OK?

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

What does the colon do in this struct definition?

It defines i to be of 1 bit width.
If i:x is given then it defines i to be x bits wide.

What does this ':' operator do in the following C++ code, and why?

As commented above, it's the number of bits to be used for each field.

struct MYMSG
{
unsigned short src : 4; // allows values 0 - 15
unsigned short dst : 11; // allows values 0 - 2047
unsigned short tx : 1; // allows values 0 - 1
};

This also has the effect of packing the structure if alignment is turned off. If this structure is not padded, then a call to sizeof() will return 2 (on an 8-bit/byte architecture).

In this case, a single unsigned short is allocated, and the bit fields are divided up within that value. Setting a value outside the range of one of the fields (such as 16 for src) will cause an overflow of that particular field, but will not alter the values of any other fields (the value of dst will not change).

Take a slightly more obtuse example:

struct T
{
unsigned long val : 4
};

This still allocates a full unsigned long (32bit on most architectures), but only allows for setting of the first 4 bits giving valid values of 0-15.



Related Topics



Leave a reply



Submit