Masking bits within a range given in parameter in C

前端 未结 4 931
忘了有多久
忘了有多久 2020-12-07 05:55

I am new to C programming and not sure that there is already a good explanation for how to do this, if so I am sorry. I am trying to set the bits within a range given to me.

4条回答
  •  臣服心动
    2020-12-07 06:02

    If its inclusive

    mask = ~(~0 << (end - start + 1));
    value = (n >> start) & mask;
    

    where n is the original integer, and value is the extracted bits.

提交回复
热议问题