Extracting bits using bit manipulation

后端 未结 2 667
灰色年华
灰色年华 2021-01-22 21:34

I have a 32-bit unsigned int and I need to extract bits at given positions and make a new number out of those bits. For example, if I have a 0xFFFFFFFF and want bits 0,10,11 my

2条回答
  •  日久生厌
    2021-01-22 22:24

    Beware, untested code:

    for(i = 0; i < count; i++) 
    {
        bitmask = 1 << positions[i];
        bit = (bytes & bitmask)!=0;
        result = (result << 1)|bit;
    }
    

提交回复
热议问题