fork(3) download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. unsigned long timeAdd (unsigned long x, unsigned long y) {
  6. // no sanity checking of input
  7. // format is hhmmssff with ff being decimal fractions of a second
  8. // "out of range" results are e.g. A0000000 for 100 hours
  9. unsigned long binsum = x + y;
  10. unsigned long carry = ((binsum + 0x06A6A666) ^ x ^ y) & 0x11111110;
  11. return (binsum + ((carry - (carry>>4)) & 0x06A6A666));
  12. }
  13.  
  14. unsigned long timeSub (unsigned long x, unsigned long y) {
  15. // no sanity checking of input
  16. // format is hhmmssff with ff being decimal fractions of a second
  17. // "negative" results are e.g. F9595999 for -0.01 second
  18. unsigned long bindiff = x - y;
  19. unsigned long borrow = (bindiff ^ x ^ y) & 0x11111110;
  20. return (bindiff - ((borrow - (borrow>>4)) & 0x06A6A666) );
  21. }
  22.  
  23.  
  24. int main() {
  25. // hhmmssff
  26. unsigned long t = 0x00000000;
  27. unsigned long u = 0x00000000;
  28. unsigned long v = 0x01234567;
  29. unsigned long step = 0x00000001;
  30. unsigned long i;
  31. for (i=1; i<=502567; i++) {
  32. t = timeAdd(t, step);
  33. u = timeAdd(step, u);
  34. v = timeSub(v, step);
  35. if (t!=u) cout << "ERROR I " << i << "\n";
  36. if (timeAdd(u, v)!=0x01234567) cout << "ERROR II " << i << "\n";
  37. if (timeSub(0x01234567, u)!=v) cout << "ERROR III " << i << "\n";
  38. if (i > 502557) {
  39. cout << hex << setw(8) << t << " ";
  40. cout << hex << setw(8) << u << " ";
  41. cout << hex << setw(8) << v << "\n";
  42. }
  43. }
  44.  
  45. // your code goes here
  46. return 0;
  47. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
 1234558  1234558        9
 1234559  1234559        8
 1234560  1234560        7
 1234561  1234561        6
 1234562  1234562        5
 1234563  1234563        4
 1234564  1234564        3
 1234565  1234565        2
 1234566  1234566        1
 1234567  1234567        0