3

I am doing some bit-fiddling that only works on a 2s-complement CPU. I would like to add a static_assert to the code, just in case. (Wikipedia Ones' complement page says that only some Univac machines still use 1s-complement, but I want to be paranoid.) Is this code bullet-proof, standard-conforming, and reasonable?

static_assert(~0 == -1, "2s-complement CPU required");
// note the tilde versus the minus sign

I do not have access to a 1s-complement machine. Is anyone with such willing to test this?

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • 6
    Should be noted that C++20 requires 2s complement. – ChrisMM Mar 11 '20 at 16:13
  • 1
    Adding to previous comment, it is http://wg21.link/P0907 – Alex Guteniev Mar 11 '20 at 16:14
  • `static_assert(true, "2s-complement CPU required");` :P – HolyBlackCat Mar 11 '20 at 16:19
  • @HolyBlackCat: yes, your condition `true` is correct for C++20, as @ChrisMM said, but (a) I want a test that works for older versions; (b) if any 1s complement compiler still exists, it (by definition) is not C++20 compliant; (c) it seems `true` could be replaced by something like `__cplusplus >= 202000L` but it appears that older compilers do not consistently support `__cplusplus` [link](https://stackoverflow.com/questions/2324658/how-to-determine-the-version-of-the-c-standard-used-by-the-compiler) – Paul McQuesten Apr 21 '21 at 21:40

0 Answers0