Search code examples
cintel-fpga

Why does printf with %lld return a different number than with %16x when using 64 bit (long long)?


I am accessing memory on a FPGA from a HPS running Linux and I stumbled upon a problem.

    {
        long long address_debug = *(shared_memory + i);
        printf("index: %i - value: %16x \n", i, address_debug);
    }

returns the values that I expect in a hexadecimal format, whereas


    for (i = 0; i < 700; i++)
    {
        long long address_debug = *(shared_memory + i);

            printf("index: %i - value: %lld \n", i, address_debug);
    }

returns values that are shifted 32 bit to the left. I get correct results with:

printf("index: %i - value: %lld \n", i, address_debug>>31);

or

printf("index: %i - value: %llu \n", i, address_debug>>31);

I am confused, as the variable itself has the same value, what am I missing?


Solution

  • When you use "%16x", printf handles the given value as unsigned int.

    Please, tell to printf that value is long long by using: "%16llx"

    From man page of printf:

    ll

    (ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument, or a following n conversion corresponds to a pointer to a long long int argument.