Search code examples
csignedtwos-complementones-complement

How to detect encodings on signed integers in C?


The ISO C standard allows three encoding methods for signed integers: two's complement, one's complement and sign/magnitude.

What's an efficient or good way to detect the encoding at runtime (or some other time if there's a better solution)? I want to know this so I can optimise a bignum library for the different possibilities.

I plan on calculating this and storing it in a variable each time the program runs so it doesn't have to be blindingly fast - I'm assuming the encoding won't change during the program run :-)


Solution

  • You just have to check the low order bits of the constant -1 with something like -1 & 3. This evaluates to

    1. for sign and magnitude,
    2. for one's complement and
    3. for two's complement.

    This should even be possible to do in a preprocessor expression inside #if #else constructs.