Why does 1ul << 64 return 1 instead of 0? : <p>Consider the following piece of code:</p>

<pre><code>// Simply loop over until 64 is hit. 
unsigned long x = 0;
for (int i = 0; i &lt;= 64; i++) {
  if (i == 64) {
    x = 1ul &lt;&lt; i;
    printf("x: %d\n", x);
  }
}
</code></pre>

<p>We know that unsigned long is 64-bit wide, and left shifting 1 by 64 positions would become 1000...000 (64 zeros behind one), and would have been truncated to 0. However, the actual printout gives:</p>

<pre><code>x: 1
</code></pre>

<p>The strange thing is, if we just do </p>

<pre><code>printf("x: %d\n", (1ul &lt;&lt; 64));
</code></pre>

<p>It would print 0. </p>

<p>Can anyone explain why this is happening? Why in the first case, the program mistakenly produces 1 instead of 0, but in the second case it's correct? </p>
