1  #include <limits.h>
       2  
       3  extern void abort ();
       4  
       5  int test1(int x)
       6  {
       7    return ~(x ^ INT_MIN);
       8  }
       9  
      10  unsigned int test1u(unsigned int x)
      11  {
      12    return ~(x ^ (unsigned int)INT_MIN);
      13  }
      14  
      15  unsigned int test2u(unsigned int x)
      16  {
      17    return ~(x + (unsigned int)INT_MIN);
      18  }
      19  
      20  unsigned int test3u(unsigned int x)
      21  {
      22    return ~(x - (unsigned int)INT_MIN);
      23  }
      24  
      25  int test4(int x)
      26  {
      27    int y = INT_MIN;
      28    return ~(x ^ y);
      29  }
      30  
      31  unsigned int test4u(unsigned int x)
      32  {
      33    unsigned int y = (unsigned int)INT_MIN;
      34    return ~(x ^ y);
      35  }
      36  
      37  unsigned int test5u(unsigned int x)
      38  {
      39    unsigned int y = (unsigned int)INT_MIN;
      40    return ~(x + y);
      41  }
      42  
      43  unsigned int test6u(unsigned int x)
      44  {
      45    unsigned int y = (unsigned int)INT_MIN;
      46    return ~(x - y);
      47  }
      48  
      49  
      50  
      51  void test(int a, int b)
      52  {
      53    if (test1(a) != b)
      54      abort();
      55    if (test4(a) != b)
      56      abort();
      57  }
      58  
      59  void testu(unsigned int a, unsigned int b)
      60  {
      61    if (test1u(a) != b)
      62      abort();
      63    if (test2u(a) != b)
      64      abort();
      65    if (test3u(a) != b)
      66      abort();
      67    if (test4u(a) != b)
      68      abort();
      69    if (test5u(a) != b)
      70      abort();
      71    if (test6u(a) != b)
      72      abort();
      73  }
      74  
      75  
      76  int main()
      77  {
      78  #if INT_MAX == 2147483647
      79    test(0x00000000,0x7fffffff);
      80    test(0x80000000,0xffffffff);
      81    test(0x12345678,0x6dcba987);
      82    test(0x92345678,0xedcba987);
      83    test(0x7fffffff,0x00000000);
      84    test(0xffffffff,0x80000000);
      85  
      86    testu(0x00000000,0x7fffffff);
      87    testu(0x80000000,0xffffffff);
      88    testu(0x12345678,0x6dcba987);
      89    testu(0x92345678,0xedcba987);
      90    testu(0x7fffffff,0x00000000);
      91    testu(0xffffffff,0x80000000);
      92  #endif
      93  
      94  #if INT_MAX == 32767
      95    test(0x0000,0x7fff);
      96    test(0x8000,0xffff);
      97    test(0x1234,0x6dcb);
      98    test(0x9234,0xedcb);
      99    test(0x7fff,0x0000);
     100    test(0xffff,0x8000);
     101  
     102    testu(0x0000,0x7fff);
     103    testu(0x8000,0xffff);
     104    testu(0x1234,0x6dcb);
     105    testu(0x9234,0xedcb);
     106    testu(0x7fff,0x0000);
     107    testu(0xffff,0x8000);
     108  #endif
     109  
     110    return 0;
     111  }
     112