fork download
  1. #include <inttypes.h>
  2. #include <stdio.h>
  3.  
  4. typedef union
  5. {
  6. struct
  7. {
  8. #define BITG(n) uint8_t bit##n : 1
  9. BITG(0);
  10. BITG(1);
  11. BITG(2);
  12. BITG(3);
  13. BITG(4);
  14. BITG(5);
  15. BITG(6);
  16. BITG(7);
  17. #undef BITG
  18. } bits;
  19. uint8_t value;
  20. }getbit;
  21.  
  22. uint8_t bit_sum(uint8_t, uint8_t);
  23.  
  24.  
  25. uint8_t bit_sum(uint8_t a, uint8_t b)
  26. {
  27. getbit op1, op2, opr;
  28. uint8_t carry;
  29. op1.value=a; op2.value=b;
  30. #define OP1(n) op1.bits.bit##n
  31. #define OP2(n) op2.bits.bit##n
  32. #define OPR(n) opr.bits.bit##n
  33. #define XOR(a,b) ((a)^(b))
  34. #define AND(a,b) ((a)&(b))
  35. OPR(0) = XOR(OP1(0), OP2(0));
  36. carry = AND(OP1(0), OP2(0));
  37. #define SETBIT(n) \
  38.   OPR(n) = XOR \
  39.   ( \
  40.   carry, \
  41.   XOR(OP1(n), OP2(n)) \
  42.   );
  43.  
  44. #define CARRYBIT(n) \
  45.   carry = XOR \
  46.   ( \
  47.   AND(OP1(n), OP2(n)), \
  48.   AND \
  49.   ( \
  50.   XOR(OP1(n), OP2(n)), \
  51.   carry \
  52.   ) \
  53.   );
  54. SETBIT(1);
  55. CARRYBIT(1);
  56. SETBIT(2);
  57. CARRYBIT(2);
  58. SETBIT(3);
  59. CARRYBIT(3);
  60. SETBIT(4);
  61. CARRYBIT(4);
  62. SETBIT(5);
  63. CARRYBIT(5);
  64. SETBIT(6);
  65. CARRYBIT(6);
  66. SETBIT(7);
  67. return opr.value;
  68. #undef SETBIT
  69. #undef CARRYBIT
  70. #undef OP1
  71. #undef OP2
  72. #undef OPR
  73. #undef XOR
  74. #undef AND
  75. }
  76.  
  77. int main (int argc, char *argv[], char *envp[])
  78. {
  79. uint8_t a, b, c;
  80. scanf ("%"SCNu8"%"SCNu8, &a, &b);
  81. c = bit_sum(a,b);
  82. printf("%"PRIu8"\n", c);
  83. return 0;
  84. }
Success #stdin #stdout 0s 2056KB
stdin
123 123
stdout
246