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

0

2 Answers 2

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' 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.