#include <iostream>
#include <iomanip>
using namespace std;
unsigned long timeAdd (unsigned long x, unsigned long y) {
// no sanity checking of input
// format is hhmmssff with ff being decimal fractions of a second
// "out of range" results are e.g. A0000000 for 100 hours
unsigned long binsum = x + y;
unsigned long carry = ((binsum + 0x06A6A666) ^ x ^ y) & 0x11111110;
return (binsum + ((carry - (carry>>4)) & 0x06A6A666));
}
unsigned long timeSub (unsigned long x, unsigned long y) {
// no sanity checking of input
// format is hhmmssff with ff being decimal fractions of a second
// "negative" results are e.g. F9595999 for -0.01 second
unsigned long bindiff = x - y;
unsigned long borrow = (bindiff ^ x ^ y) & 0x11111110;
return (bindiff - ((borrow - (borrow>>4)) & 0x06A6A666) );
}
int main() {
// hhmmssff
unsigned long t = 0x00000000;
unsigned long u = 0x00000000;
unsigned long v = 0x01234567;
unsigned long step = 0x00000001;
unsigned long i;
for (i=1; i<=502567; i++) {
t = timeAdd(t, step);
u = timeAdd(step, u);
v = timeSub(v, step);
if (t!=u) cout << "ERROR I " << i << "\n";
if (timeAdd(u, v)!=0x01234567) cout << "ERROR II " << i << "\n";
if (timeSub(0x01234567, u)!=v) cout << "ERROR III " << i << "\n";
if (i > 502557) {
cout << hex << setw(8) << t << " ";
cout << hex << setw(8) << u << " ";
cout << hex << setw(8) << v << "\n";
}
}
// your code goes here
return 0;
}