How to extract bit from uint32_t in c++

Viewed 35

My task is to write a function which will return given amount of bits with desired shift.

I tried writing this function but it doesn't give me result that is expected.

"uint8_t b" is a starting point from where I want to extract bits,

and

"uint8_t c" is the length of bits I want to extract.

uint32_t getbitsfromuint32(uint32_t a,uint8_t b,uint8_t c)
{
    unsigned mask;
    mask = ((1<<c)-1)<<b;
    uint32_t isolatedXbits = a & mask;
    
    return isolatedXbits;
}

int main()
{
    uint32_t res = getbitsfromuint32(0xABCDEF12, 4, 12);
    
    std::cout<<res<<std::endl;
}

the result I get is res = 61200;

when it is suppose to be res = 0x00000EF1;

What am I doing wrong with my code?

0 Answers
Related