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 returns an integer where all the bits are set to the same value as the MSB.
But I'd like to do it in a completely constant time way, no branches, no array offsets, just mathematical operations which are guaranteed to always touch the same number of bits. And ideally, without any undefined behavior. How can this be done?