title: Implicit conversion or cast?
body: <p>I have a function that interleaves the bits of 32 bit words and returns a 64 bit result. For this simple test case, the bottom 3 bytes are correct, and the contents of the top 5 bytes are incorrect. intToBin_32 and intToBin_64 are convenience functions to see the binary representation of the arguments and return val. I've placed casts from the 32 bit type to the 64 bit type everywhere I think they are needed, but I'm still seeing this unexpected (to me, at least) behavior. Is there an implicit conversion going on here, or is there some other reason this doesn't work correctly?</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  
  struct intString_32 {char bstr [32 + 1 + 8];};
  struct intString_64 { char bstr [64 + 1 + 8];};
  
  intString_32 intToBin_32(int a)
  {
      intString_32 b;
  
      for (int i = 0; i &lt; 8; i++)
      {
          for (int j = 0; j &lt; 5; j++)
          {
              if (j != 4)
              {
                  b.bstr[5*i + j] = * ((a &amp; (1 &lt;&lt; (31 - (4*i + j)))) ? "1" : "0");
              }
  
              else
              {
                  b.bstr[5*i + j] = 0x20;
              }
          }
      }
  
      b.bstr[40] = * ( "\0" );
  
      return b;
  }
  
  intString_64 intToBin_64(long long a)
  {
      intString_64 b;
  
      for (int i = 0; i &lt; 8; i++)
      {
          for (int j = 0; j &lt; 9; j++)
          {
              if (j != 8)
              {
                  b.bstr[9*i + j] = * ((a &amp; (1 &lt;&lt; (63 - (8*i + j)))) ? "1" : "0");
              }
  
              else
              {
                  b.bstr[9*i + j] = 0x20;
              }
          }
      }
  
      b.bstr[72] = * ( "\0" );
      return b;
  }
  
  uint64_t interleaveBits(unsigned int a, unsigned int b)
  {
      uint64_t retVal = 0;
  
      for (unsigned int i = 0; i &lt; 32; i++)
      {
          retVal |= (uint64_t)((uint64_t)((a &gt;&gt; i) &amp; 0x1)) &lt;&lt; (2*i);
          retVal |= (uint64_t)((uint64_t)((b &gt;&gt; i) &amp; 0x1)) &lt;&lt; (2*i + 1);
      }
  
      return retVal;
  }
  
  int main(int arc, char* argv)
  {
      unsigned int foo = 0x0004EDC7;
      unsigned int bar = 0x5A5A00FF;
      uint64_t bat = interleaveBits(foo, bar);
  
      printf("foo: %s \n", intToBin_32(foo).bstr);
      printf("bar: %s \n", intToBin_32(bar).bstr);
      printf("bat: %s \n\n", intToBin_64(bat).bstr);
  }
  </code></pre>