code :
s64 end_time;
struct timespec ts;
getrawmonotonic(&ts);
end_time = timespec_to_ns(&ts);
How to remove the first three bytes from end_time
and last one byte from it??
I want to store it in a uint32.
could someone tell me how to do that??
uint32 latency;
fscanf(fp, "%lu\n", latency); //fp is reading the end_time and storing in latency.
latency = (uint32) (latency >> 8) & 0xFFFFFFFF;
You can do that with bit shifting. You have to shift the value 8 bits (= 1 byte) to the right, which is done with the
>>
operator:In the following, the bytes are visualized for a better understanding. If the value
end_time
consisted of eight bytes with the symbolic valuesA B C D E F G H
, what you want isD E F G
:How about:
Depending on your definition of
first
andlast
byte it could also be:Example:
Edit
After your updated question:
I assume by "first" and "last" you mean "most significant" and "least significant", respectively.
I.e., you have the 8 bytes:
and want to map it to the 4 bytes:
This is easiest done by a shift, a mask, and a (truncating) cast:
The mask is very likely to be optimized out by the compiler but makes it very clear what's going on.