1

Here is some code.

long long bitmap2 = 1;
printf("%d\n", bitmap2 & 1);

The output is 0, but I am expecting 1. How can I fix this? (I have tried 1LL instead of 1 and uint64_t instead of long long; both gave the same answer of 0.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

What you're currently seeing is undefined behaviour; you need to ensure that the format specifier and the argument match. So use one of the following:

printf("%lld\n", bitmap2 & 1);
printf("%d\n", (int)(bitmap2 & 1));

See e.g. http://en.cppreference.com/w/c/io/fprintf for complete documentation for printf.

However, most compilers should warn you if there's a mismatch between the printf format string and the types of the arguments that you supply. For example, if I compile your code in GCC with the -Wall flag, I get the following:

warning: format '%d' expects type 'int', but argument 2 has type 'long long int'
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

I think you should use printf("%d\ll", bitmap2 & 1) instead of printf("%d\n", bitmap2 & 1) if you want to print a long long.

Erica Xu
  • 545
  • 1
  • 4
  • 13