fork(1) download
  1. #include <string>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <algorithm>
  5.  
  6. /*
  7.  
  8. 79 蟻人間 ◆T6xkBnTXz7B0 2020/12/13(日) 08:24:11.25 ID:zFOj0dh9
  9.   お題:
  10.   符号なし演算ができない言語X2において、2個の32ビットの符号付き整数x, yがあたえられる。x, yを符号なし整数x', y'と見なしたときの比較演算 x' < y' の値を求めよ。
  11.  
  12. */
  13.  
  14. // uint32 x < y
  15. // 「比較演算 x' < y' の値」はC言語準拠の 0, 1
  16. int32_t i32_cmp( int32_t x, int32_t y )
  17. {
  18. int32_t rval = (0x7FFFFFFF & (x>>1)) - (0x7FFFFFFF & (y>>1));
  19. if( rval == 0 )
  20. rval = (1 & x) - (1 & y);
  21. return ( rval < 0 )? 1 : 0;
  22. }
  23.  
  24. void test( int32_t n1, int32_t n2 )
  25. {
  26. int ans = (uint32_t) n1 < (uint32_t) n2;
  27. printf( " %11ds %11uu 0x%08X : %11ds %11uu 0x%08X => %d %d : %s\n",
  28. n1, n1, n1,
  29. n2, n2, n2,
  30. ans,
  31. i32_cmp( n1, n2 ),
  32. (ans == i32_cmp( n1, n2 ))? "OK" : "NG"
  33. );
  34. }
  35.  
  36. int main(void)
  37. {
  38. test( 0, 0 );
  39. test( 0, 1 );
  40. test( 1, 0 );
  41. test( 1, 1 );
  42.  
  43. test( 123, 123 );
  44. test( 123, 456 );
  45. test( 456, 123 );
  46. test( 456, 456 );
  47.  
  48. test( 0, -1 );
  49. test( -1, 0 );
  50. test( -1, -1 );
  51.  
  52. test( 1, -1 );
  53. test( -1, 1 );
  54.  
  55. return 0;
  56. }
  57.  
  58.  
Success #stdin #stdout 0s 4708KB
stdin
Standard input is empty
stdout
           0s           0u  0x00000000  :            0s           0u  0x00000000  =>  0  0 : OK
           0s           0u  0x00000000  :            1s           1u  0x00000001  =>  1  1 : OK
           1s           1u  0x00000001  :            0s           0u  0x00000000  =>  0  0 : OK
           1s           1u  0x00000001  :            1s           1u  0x00000001  =>  0  0 : OK
         123s         123u  0x0000007B  :          123s         123u  0x0000007B  =>  0  0 : OK
         123s         123u  0x0000007B  :          456s         456u  0x000001C8  =>  1  1 : OK
         456s         456u  0x000001C8  :          123s         123u  0x0000007B  =>  0  0 : OK
         456s         456u  0x000001C8  :          456s         456u  0x000001C8  =>  0  0 : OK
           0s           0u  0x00000000  :           -1s  4294967295u  0xFFFFFFFF  =>  1  1 : OK
          -1s  4294967295u  0xFFFFFFFF  :            0s           0u  0x00000000  =>  0  0 : OK
          -1s  4294967295u  0xFFFFFFFF  :           -1s  4294967295u  0xFFFFFFFF  =>  0  0 : OK
           1s           1u  0x00000001  :           -1s  4294967295u  0xFFFFFFFF  =>  1  1 : OK
          -1s  4294967295u  0xFFFFFFFF  :            1s           1u  0x00000001  =>  0  0 : OK