0

I'm quite new to programming, I have recently learnt a little C++ and I am using Visual Studio 2017 Community version.

I need to use a 64 bit integer to store a value and carry out some arithmetic operations, however my compiler only allows me to use 32 bits of the "int64" variable I have created.

Here is an example of some code and the behaviour

    unsigned __int64  testInt = 0x0123456789ABCDEF;

printf("int value = %016X\n", testInt); // only 32 bits are being stored here? (4 bytes)

printf("size of integer in bytes = %i\n\n", sizeof(testInt)); // size of int is correct (8 bytes)

The value stored in the variable seems to be 0x0000000089ABCDEF. Why can I not use all 64 bits of this integer, it seems to act as a 32 bit int?

Probably I'm missing something basic, but I can't find anything relating to this from searching :(

Reggie
  • 1
  • 1
  • 1
    You need to use `%016llX` instead of `%016X`. The `%X` expects a 32-bit integer. – Jonesinator Sep 17 '17 at 00:05
  • 1
    The `%X` format assumes a stock-standard `unsigned` (which may not be 64-bit), not a `unsigned __int64`. From memory, an `unsigned` in VS2017 is 32-bit. You need to use `ll` prefix to get `long long unsigned` – Peter Sep 17 '17 at 00:05
  • 1
    There are 64-bit specific format specifiers defined in . See https://stackoverflow.com/a/9225648/925478 – Myk Willis Sep 17 '17 at 00:06
  • 1
    Use a debugger to determine the actual contents of a variable. – Scott Hunter Sep 17 '17 at 00:07
  • @Reggie I'm glad it worked, but for future reference, it's is very much advisable to learn to write portable code, which means dealing with the macros and `"%016" PRIX64` – Darael Sep 17 '17 at 00:35

2 Answers2

2

It would be nice if it were just something basic, but it turns out that 64 bit ints are not dealt with consistently on all platforms so we have to lean on macros.

This answer describes the use of PRIu64, PRIx64, and related macros included in <inttypes.h>. It looks funny like this, but I think the portable solution would look like:

#include <inttypes.h>
unsigned __int64  testInt = 0x0123456789ABCDEF;
printf("int value = %016" PRIX64 "\n", testInt);

The PRIX64 expands to the appropriate format specifier depending on your platform (probably llX for Visual Studio).

Myk Willis
  • 12,306
  • 4
  • 45
  • 62
  • `PRIX64`, not `PRIx64` - OP's original code wants uppercase-hex output. According to the link in that answer, the two macros are distinct. – Darael Sep 17 '17 at 00:13
0

Format specifier %X takes an unsigned int (probably 32 bit on your system), whereas __int64 corresponds to long long.

Use printf("int value = %016llX\n", testInt) instead. Documentation can be found, for example, at cppreference.com.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • For maximum portability (so, to write portable C++ rather than Visual C++ for x86_64), would it not be best to use `printf("int value = %016" PRIX64 "\n", testInt)` per @MykWillis's link? – Darael Sep 17 '17 at 00:12
  • 1
    @Darael: probably yes; though I suppose that `unsigned __int64` is not portable as well... – Stephan Lechner Sep 17 '17 at 00:32