How do I extract specific 'n' bits of a 32-bit unsigned integer in C?

前端 未结 8 2355
庸人自扰
庸人自扰 2020-12-02 08:02

Could anyone tell me as to how to extract \'n\' specific bits from a 32-bit unsigned integer in C.

For example, say I want the first 17 bits of the 32-bit value;

8条回答
  •  情歌与酒
    2020-12-02 08:44

    If you need the X last bits of your integer, use a binary mask :

    unsigned last8bitsvalue=(32 bit integer) & 0xFF
    unsigned last16bitsvalue=(32 bit integer) & 0xFFFF
    

提交回复
热议问题