user3635707
user3635707

Reputation: 5

why am I getting a negative value in taking a timestamp?

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

Answers (1)

Marian
Marian

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

Related Questions