0

The title sums it up: left-shifting an uint64_t doesn't output the expected value, and I'd like to know why. What I get is the expected result with its 4 most significant bytes zeroed out. I'm using an x86_64 CPU (Intel i7 3770k), on Debian Jessie 64bit. This is a test program that reproduces the same behavior.

#include <stdio.h>
#include <stdint.h>

int main(int argc, char * * argv)
{
    uint64_t var = 0xDEFEC8ED;

    printf("%016x\n", var << 24);

    return 0;
}

OUTPUT 00defec8ed000000 // expected 00000000ed000000 // obtained

pr0gma
  • 577
  • 3
  • 8
  • 18

2 Answers2

3

It is uint64_t and you need a different type for the printf

#include <stdio.h>
#include <stdint.h>

int main(int argc, char * * argv)
{
    uint64_t var = 0xDEFEC8ED;

    printf("%016llx\n", (unsigned long long)(var << 24));

    return 0;
}

Which is not fully correct because there are special macros for printing those types since C99:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(int argc, char * * argv)
{
    uint64_t var = 0xDEFEC8ED;

    printf("%016" PRIX64 "\n", var << 24);

    return 0;
}

But you may or may not have them.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Thanks, it works, and accepted for having mentioned those special macros I didn't know about. – pr0gma Oct 14 '16 at 15:00
1

Replace:

printf("%016x\n", var << 24);

with:

printf("%016llx\n", (var << 24));

Also, is it uint_64 or uint64_t ?

scorpGoku
  • 118
  • 2
  • 10