Bit mask in C

前端 未结 5 1478
情深已故
情深已故 2021-01-30 09:52

What is the best way to construct a bit mask in C with m set bits preceded by k unset bits, and followed by n unset bits:

         


        
5条回答
  •  孤独总比滥情好
    2021-01-30 09:53

    So, you are asking for m set bits prefixed by k reset bits and followed by n reset bits? We can ignore k since it will largely be constrained by the choice of integer type.

    mask = ((1 << m) - 1) << n;
    

提交回复
热议问题