Reputation: 5
Uint64_t a;
Uint32 b;
a= clock_cycles();
b= uint32((a*1000000)/(SYSPAGE_ENTRY(qtime)->cycles_per_sec));
printf("RECEIVE from Time in microseconds: %ld\n", b);
I created the variable and taking the timestamp and converting that into uint32 as shown in the above code.
If I print the b value then I am getting negative value!! What's wrong in doing like above ??
Upvotes: 0
Views: 1139
Reputation: 7482
Your b is larger than 2^31. Printf format "%ld" indicates you are going to print a signed integer and printf interprets numbers with highest bit as negative integers. Use "%lu" instead of "%ld".
Also when looking at your code, the numeric value of a*1000000
is very probable to overflow over maximal possible value. In your case, I'd suggest to use another function to get microsecond time, for example gettimeofday
and to store the result in a 64-bit integer.
Upvotes: 1