fork download
  1. #include <stdio.h>
  2.  
  3. int
  4. main()
  5. {
  6. char s1[] = "10110001";
  7. char s2[] = "00101110";
  8. char *p1, *p2;
  9. int carry;
  10.  
  11. printf(" %s\n- %s\n----------\n", s1, s2);
  12.  
  13. p2 = s2;
  14. while (*p2) {
  15. if (*p2 == '0')
  16. *p2 = '1';
  17. else
  18. *p2 = '0';
  19. /* *p2 = (*p2 == '0' ? '1' : '0'); */
  20. p2++;
  21. }
  22.  
  23. for (p1 = s1; *p1; p1++);
  24. for (p2 = s2; *p2; p2++);
  25.  
  26. carry = 1;
  27. while (s1 < p1) {
  28. p1--;
  29. p2--;
  30. carry += (*p1 - '0') + (*p2 - '0');
  31. *p2 = '0' + (carry % 2);
  32. carry /= 2;
  33. }
  34.  
  35. printf("= %s\n", s2);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.02s 1720KB
stdin
Standard input is empty
stdout
  10110001
- 00101110
----------
= 10000011