Reputation: 629
I am trying to copy two 32bit values into one 64bit address. One DWORD is the high 32 bits and the second is the low 32 bits. I tried the following but the shift operand in the 64bit values doesn't work:
UINT64 a = 0x12341234;
UINT64 b = 0x00003800;
The value I need is: 0x0000380012341234
So I did:
printf("b 0x%016x\n", b);
b = ((UINT64)b) << 20;
printf("b 0x%016x\n", b);
and the prints I get:
b 0x0000000000003800
b 0x0000000080000000
I tried to shift to different numbers (10 and 10 again, or 32, or 0x20) but nothing worked for me.
Why the shift to the high 32bit of the UINT64 doesn't work, and how else can I do it?
Upvotes: 0
Views: 162
Reputation: 393
To add to Vishwajeet's post, one should of course add in a:
#include <stdio.h>
#include <stdlib.h>
#define UINT64 unsigned long long
int main()
{
UINT64 a = 0x12341234;
UINT64 b = 0x00003800;
printf("b 0x%016llx\n", (unsigned long long)b);
b <<= 32;
b += a;
printf("b 0x%016llx\n",(unsigned long long) b);
system("pause");
}
Upvotes: 0
Reputation: 492
Its only printf() type problem. I tried it it works fine.
#include <stdio.h>
#include <stdlib.h>
#define UINT64 unsigned long long
int main()
{
UINT64 a = 0x12341234;
UINT64 b = 0x00003800;
printf("b 0x%016llx\n", (unsigned long long)b);
b = ((UINT64)b) << 32;
printf("b 0x%016llx\n",(unsigned long long) b);
system("pause");
}
Hope it helps. :)
Upvotes: 2
Reputation: 86651
Your format specifier tells printf
that the argument is a normal (32 bit probably) int
so it cuts off the top 32 bits (because your computer must be little endian).
You could use %llx
but, if so, I'd be tempted to also cast the argument to unsigned long long
.
printf("b 0x%016llx\n", (unsigned long long)b);
C11 also defines some fixed width format specifiers in inttypes.h
to make it safe without the cast - PRIx64
is the one you would need.
Upvotes: 3