bit fields that do not fit in a built-in type

A

Angel Tsankov

Does the C++ standard define what should happen if the size of int in the following code cannot accommodate the bit field?

struct bit_fields
{
unsigned int digit:17; // int is 16 bits
};
 
K

Kai-Uwe Bux

Angel said:
Does the C++ standard define what should happen if the size of int in the
following code cannot accommodate the bit field?

struct bit_fields
{
unsigned int digit:17; // int is 16 bits
};

The excess bits force are used for padding:

[9.6/1] A member-declarator of the form

identifier_opt : constant-expression

specifies a bit-field; its length is set off from the bit-field name by a
colon. 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 constant-expression may be larger
than the number of bits in the object representation (3.9) 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 (3.9) of the bit-field.
[...]



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top