How to initialize bit fields using C ++ constructor?

Firstly, I do not deal with portability and I can safely assume that the continent will not change. Assuming I read the value of the equipment register, I would like to overlay this register value with bit fields so that I can refer to individual fields in the register without using bit masks.

EDIT: Fixed issues noted by GMan, and tweaked the code to be clearer for future readers.

SEE: Anders K. and Michael J below cite for a more eloquent solution.

#include <iostream>

/// \class HardwareRegister
/// Abstracts out bitfields in a hardware register.
/// \warning  This is non-portable code.
class HardwareRegister
{
   public:
      /// Constructor.
      /// \param[in]  registerValue - the value of the entire register. The
      ///                             value will be overlayed onto the bitfields
      ///                             defined in this class.
      HardwareRegister(unsigned long registerValue = 0)
      {
         /// Lots of casting to get registerValue to overlay on top of the
         /// bitfields
         *this = *(reinterpret_cast<HardwareRegister*>(&registerValue));
      }


      /// Bitfields of this register.
      /// The data type of this field should be the same size as the register
      /// unsigned short for 16 bit register
      /// unsigned long for 32 bit register.
      ///
      /// \warning Remember endianess! Order of the following bitfields are
      ///          important.
      ///          Big Endian    - Start with the most signifcant bits first.
      ///          Little Endian - Start with the least signifcant bits first.
      unsigned long field1: 8;
      unsigned long field2:16;
      unsigned long field3: 8;
}; //end class Hardware


int main()
{

   unsigned long registerValue = 0xFFFFFF00;
   HardwareRegister  testRegister(registerValue);

   // Prints out for little endianess machine
   // Field 1 = 0
   // Field 2 = 65535
   // Field 3 = 255
   std::cout << "Field 1 = " << testRegister.field1 << std::endl;
   std::cout << "Field 2 = " << testRegister.field2 << std::endl;
   std::cout << "Field 3 = " << testRegister.field3 << std::endl;
}
+3
source share
4 answers

do not do this

 *this = *(reinterpret_cast<HW_Register*>(&registerValue));

'this' :

HW_Register reg(val)
HW_Register *reg = new HW_Register(val)

'this'

/struct , ( )

.

union
{
   struct {
     unsigned short field1:2;
     unsigned short field2:4;
     unsigned short field3:2;
    ...
   } bits;
   unsigned short value;
} reg

edit: true 'register'

+5

. struct, . , , reinterpret_cast, reinterpret_cast , .

, .

    HW_Register(unsigned char value)
      : field1( value & 3 ),
        field2( value >> 2 & 3 ),
        field3( value >> 4 & 7 )
        {}

. , - , , ASCII, , . :

std::cout << "Field 1 = " << (int) testRegister.field1 << std::endl;
std::cout << "Field 2 = " << (int) testRegister.field2 << std::endl;
std::cout << "Field 3 = " << (int) testRegister.field3 << std::endl;
+5

Try the following:

class HW_Register
{
public:
    HW_Register(unsigned char nRegisterValue=0)
    {
        Init(nRegisterValue);
    }
    ~HW_Register(void){};

    void Init(unsigned char nRegisterValue)
    {
        nVal = nRegisterValue;
    }

    unsigned Field1() { return nField1; }
    unsigned Field2() { return nField2; }
    unsigned Field3() { return nField3; }

private:
    union
    {
        struct 
        {
            unsigned char nField1:2;
            unsigned char nField2:4;
            unsigned char nField3:2;
        };
        unsigned char nVal;
    };
};


int main()
{
    unsigned char registerValue = 0xFF;
    HW_Register  testRegister(registerValue);

    std::cout << "Field 1 = " << testRegister.Field1() << std::endl;
    std::cout << "Field 2 = " << testRegister.Field2() << std::endl;
    std::cout << "Field 3 = " << testRegister.Field3() << std::endl;

    return 0;
}
+2
source
HW_Register(unsigned char registerValue) : field1(0), field2(0), field3(0) 
+1
source

Source: https://habr.com/ru/post/1761454/


All Articles