Search code examples
clanguage-lawyerbit-manipulationbit

Standard way of getting a bit pattern of all ones


Is there a strictly conforming way to get a bit-pattern of all ones for an integer in C, without relying on implementation-defined behavior?

On many architectures I can expect -1 and the UINT_MAX family to give such a bit pattern; how portable is this assumption?


Solution

  • Extremely portable. The C23 standard disallows integer representations that are not 2's complement, so then it will be guaranteed portable.

    ~0 or ~0U should always work.