Reputation: 75
I have 64-bits integer:
{uint64_t st0 = 1470134824450120;}
I would like to get only the last 8-digits of this number, i.e. 24450120
I did for that the following:
uint32_t timestamp1 = 0;
uint32_t timestamp2 = 0;
timestamp2 = (uint32_t) st0;
timestamp1 = st0 >> 32;
printf("input: %" PRIu64 "\n", st0);
printf("unpacked: %" PRIu32 " %" PRIu32 "\n", timestamp2, timestamp1);
As the result I have got:
input: 1470134824450120
unpacked: 1878767688 342292
What is wrong here?
Upvotes: 1
Views: 3685
Reputation: 27428
I would think you would -and it with hex 0xffffffff to get the right half. I only know powershell (which has its own quirks). I don't think you could check the results in decimal (1878767688).
1470134824450120 | % tostring x
539146ffbb848
1470134824450120 -band [uint32]("0x{0:X}" -f 0xffffffff) | % tostring x
6ffbb848
Upvotes: 0
Reputation: 31
I have not tried compiling but you can try this :)
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
typedef union {
struct {
uint32_t a;
uint32_t b;
} decoupled;
uint64_t value;
} MyUnsignedDecoupledInt64;
#pragma pack(pop) /* restore original alignment from stack */
MyUnsignedDecoupledInt64 getDecoupledUnsignedInt64(uint64_t value)
{
MyUnsignedDecoupledInt64 ret;
ret.value = value;
return ret;
}
int main() {
MyUnsignedDecoupledInt64 t = getDecoupledUnsignedInt64(1000000000LL);
printf("%u %u\n", t.decoupled.a, t.decoupled.b);
return 0;
}
Upvotes: 0
Reputation: 1977
So you have this number: 1470134824450120.
Its represantion in binary is:
00000000000001010011100100010100 01101111111110111011100001001000
After you compututation you have in variable timestamp2 1101111111110111011100001001000 in bits which is 1878767688 in decimal and in timestamp1 00000000000001010011100100010100 which is 342292 in decimal.
So the values are correct.
If you want to get the last 8 digits: st0 % 100000000
Upvotes: 3
Reputation: 1545
Your title of the post asks for something other than you ask in your question. If you want to split/cut a specific amount of decimal digits, it can not be done with operations as if you want binary digits.
If you really want to get the last 8 decimal digits, I suggest you to put this 64-bit integer to a buffer as string (e.g. sprintf), then cut the last 8 digits from this buffer and make an integer out of it (e.g. atol).
Upvotes: 0