I have two values from an unsigned 48-bit nanosecond counter that can turn around.
I need a nanosecond difference two times.
I think I can assume that the evidence was taken at about the same time, so from the two possible answers, I think that I take the smallest amount safely.
Both of them are saved as uint64_t . Because I do not think that I can have 48-bit types.
I would like to calculate the difference between the two as a signed integer (presumably int64_t ), taking into account the wrapping.
those. if i start with
x=5 y=3
then the result of xy is 2 and will remain so if I increment both x and y even when they wrap the top of the maximum value 0xffffffffffff
Similarly, if x = 3, y = 5, then xy is -2 and will remain the same if x and y increase simultaneously.
If I could declare x , y as uint48_t , and the difference as int48_t , then I think
int48_t diff = x - y;
will work.
How to simulate this behavior with the 64-bit arithmetic I got?
(I think that any computer on which it is likely to work will use arithmetic with two additions)
PS I can probably hack this, but I wonder if there is a good standard way to do things that the next person can read my code about.
PPS In addition, this code will end in the narrowest of the hard loops, so something that will compile efficiently would be nice, so if there is a choice, it will read trump cards.
c math unsigned signed
John lawrence aspden
source share