I have an int64_t variable. When I add another int64_t variable

and convert it to a double to print it on the serial monitor, it acts as if it was a int32_t variable. This is the program:

int64_t a = 1 << 31;
double b = static_cast<double>(a);

The output on the serial monitor is -2147483648.00.

What is going on?

1 Answer 1


1 << 31 is interpreted by the compiler as (int)1 << (int)32. See the implicit types of integer literals. The rules of usual arithmetic conversions state that no implicit conversion is performed in this case. The result is then computed as an int, and it overflows. Note that this is undefined behavior. The assignment int64_t a = ... converts the (already overflowed) result to the wider type, without changing its value.

You probably want to write something like:

int64_t a = 1LL << 31;
  • 1
    even uint32_t a = 1UL << 31; would do the trick to interpret 0x80000000 as a positive number. BTW, on a real arduino, where the native int is an int16_t you get 1<<31 == 0 Jun 16, 2020 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.