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
{
public:
HardwareRegister(unsigned long registerValue = 0)
{
*this = *(reinterpret_cast<HardwareRegister*>(®isterValue));
}
unsigned long field1: 8;
unsigned long field2:16;
unsigned long field3: 8;
};
int main()
{
unsigned long registerValue = 0xFFFFFF00;
HardwareRegister 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;
}
source
share