user60405: 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);
Serial.println(b);

The output on the serial monitor is -2147483648.00.
What is going on?

user7508: 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;


60405: <p>I have an <code>int64_t</code> variable. When I add another <code>int64_t</code> variable</p>

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

<pre class="lang-cpp prettyprint-override"><code>int64_t a = 1 &lt;&lt; 31;
double b = static_cast&lt;double&gt;(a);
Serial.println(b);
</code></pre>

<p>The output on the serial monitor is <code>-2147483648.00</code>.</p>

<p>What is going on?</p>
 7508: <p><code>1 &lt;&lt; 31</code> is interpreted by the compiler as <code>(int)1 &lt;&lt; (int)32</code>.
See <a href="https://en.cppreference.com/w/cpp/language/integer_literal#The_type_of_the_literal" rel="nofollow noreferrer">the implicit types of integer literals</a>. The
rules of usual arithmetic conversions state that no implicit conversion
is performed in this case. The result is then computed as an <code>int</code>, and
it overflows. Note that this is undefined behavior. The assignment
<code>int64_t a = ...</code> converts the (already overflowed) result to the wider
type, without changing its value.</p>

<p>You probably want to write something like:</p>

<pre class="lang-cpp prettyprint-override"><code>int64_t a = 1LL &lt;&lt; 31;
</code></pre>
