It's a long time since I wrote a union. I think I had an overdose of them from a multi-platform source long time ago...
Anyway I think I would have written it like thisto not have to maintain more fields than neccessary. You don't have to have a name for unused fields and in fact in this case you don't have to have it at all. The remaining 44 bits will be present anyway since the underlying type is 64 bits.Code:typedef struct { union { uint64_t all; struct { uint64_t byteOffset : 6; uint64_t address : 12; uint64_t bank : 2; uint64_t : 44; } parts; } address; unsigned int byteCount; unsigned int src; unsigned char data; } addressMap;
As said before bitfields are not fully specified by the standard. In MSVC the byteOffset bits will be the least significant bits in 'all'. In another compiler they might be the most significant bits.




Reply With Quote