Waad
Waad

Reputation: 384

How to get the starting bit number when using bit fields (C++)

Assume we have a struct like this

struct my_struct
{
    uint16_t a : 2;
    uint16_t b : 6;
    uint16_t c : 8;
}my_struct

my_struct temp;

Is there anyway to get the starting/ending bit of temp.b? in other words, is there a syntax/built-in function in C++ that does the following:

first_function(temp.b) // returns 2, since b starts on the second bit of the struct in the computers memory
second_function(temp.b) // Returns 8, since the last bit of b is the 8th bit of the struct in the computers memory

Hope my question is clear

Upvotes: 0

Views: 178

Answers (2)

eerorika
eerorika

Reputation: 238421

No, such functions don't exist in standard library. Furthermore, the language doesn't guarantee that your assumptions about the order of the bit fields match with the language implementation.

Upvotes: 0

SergeyA
SergeyA

Reputation: 62603

Yes, your question is clear, but sadly no - such function does not exist. Perhaps, there is a way to achieve what you want to do using some other means.

Upvotes: 2

Related Questions