Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a bit field in a constructor

Tags:

c++

How do you initialize the values of the following struct in a constructor to defined values?

Both options shown in my code example seem to be a bit ugly....

struct T_AnlagenInfo01
{   
    // Option A
    T_AnlagenInfo01() : fReserve80_0(0), fReserve80_1(0),.... {}; 

    // Option B
    T_AnlagenInfo01()
    { 
        memset(this, 0, sizeof(T_AnlagenInfo01));
    } 

    unsigned long fReserve80_0                          : 1;        
    unsigned long fReserve80_1                          : 1;        
    unsigned long fReserve80_2                          : 1;        
    unsigned long fReserve80_3                          : 1;        
    unsigned long fReserve80_4                          : 1;
    unsigned long fReserve80_5                          : 1;
    unsigned long fReserve80_6                          : 1;
    unsigned long fReserve80_7                          : 1;

    unsigned long fReserve81_0                          : 1;        // 81   
    unsigned long fReserve81_1                          : 1;        
    unsigned long fReserve81_2                          : 1;        
    unsigned long fReserve81_3                          : 1;
    unsigned long fReserve81_4                          : 1;
    unsigned long fReserve81_5                          : 1;
    unsigned long fReserve81_6                          : 1;
    unsigned long fReserve81_7                          : 1;
};
like image 446
Ronald McBean Avatar asked Jan 17 '12 13:01

Ronald McBean


People also ask

How do you define a bit field?

A bit field is a data structure that consists of one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected.

Can we reference a bit field?

Pointers and non-const references to bit-fields are not possible. When initializing a const reference from a bit-field, a temporary is created (its type is the type of the bit-field), copy initialized with the value of the bit-field, and the reference is bound to that temporary.

What is bit field type in C++?

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared.


1 Answers

One obvious solution would be to put all of the bits in a separate structure, which is a member of your structure, and copy initialize that from a static member, e.g.:

struct T_AnlagenInfo01
{
    struct Bits
    {
        unsigned long fReserve80_0 : 1;
        unsigned long fReserve80_1 : 1;
        //  ...
    };
    Bits myBits;
    static Bits initialBits;

    T_AnlagenInfo01 : myBits(initialBits) {}
};

T_AnlagenInfo01::Bits T_AnlagenInfo01::initialBits = {};

This would even allow for certain bits to have a value different from 0.

like image 97
James Kanze Avatar answered Oct 17 '22 17:10

James Kanze