How to write a constant time function to copy the most significant bit to all bits

后端 未结 3 1745
执笔经年
执笔经年 2021-02-14 19:09

I\'d like to write a function, in C, which takes the MSB of uint8_t, and if it\'s set, returns 0xFF and if not 0x00. In short, which retur

3条回答
  •  温柔的废话
    2021-02-14 19:36

    What about

    - (x >> 7)
    

    ?

    Only the MSB is retained and math negation is used to replicate it to all bits.

提交回复
热议问题