16

What is the most reliable way to detect whether the architecture uses one's or two's complement representation in C++?

Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 4
    If those are the only options, check `(-1) & 1`. – Daniel Fischer May 11 '13 at 19:46
  • 1
    @BasileStarynkevitch I work with heterogeneous supercomputer architectures and I want to record that at the beginning of each binary file to know whether it is compatible with the architecture that will try to read it. – Vincent May 11 '13 at 19:48
  • doesn't little or big endianness matter much more today? And basically you are doing serialization, and there are libraries to do that reliably today..... [s11n](http://s11n.net/) and others – Basile Starynkevitch May 11 '13 at 19:52
  • 2
    @Vincent - just compare "-1" with "~0". They're *equal* with twos complement, they are *not equal* with ones complement. – paulsm4 May 11 '13 at 19:53

1 Answers1

13

You shouldn't have to worry - there aren't too many ones complement machines out there :)

But the easiest thing might be to compare "-1" with ~0.

paulsm4
  • 114,292
  • 17
  • 138
  • 190