0

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?

nobody
  • 19,814
  • 17
  • 56
  • 77
user1016179
  • 599
  • 3
  • 10
  • 24

3 Answers3

3

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

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. :)

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Vishwajeet Vishu
  • 492
  • 3
  • 16
0

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");
}
Peter
  • 393
  • 3
  • 15