title: If Statement True Block Executed When Condition is False
body: <p>I optimized an extension method to compare two streams for equality (byte-for-byte) - knowing that this is a hot method I tried to optimize it as far as possible (the streams can reach into multi-megabyte lengths). I essentially came up with the following approach:</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit)]
  struct Converter
  {
      [FieldOffset(0)]
      public Byte[] Byte;
  
      [FieldOffset(0)]
      public UInt64[] UInt64;
  }
  
  /// &lt;summary&gt;
  /// Compares two streams for byte-by-byte equality.
  /// &lt;/summary&gt;
  /// &lt;param name="target"&gt;The target stream.&lt;/param&gt;
  /// &lt;param name="compareTo"&gt;The stream to compare the target to.&lt;/param&gt;
  /// &lt;returns&gt;A value indicating whether the two streams are identical.&lt;/returns&gt;
  public static bool CompareBytes(this Stream target, Stream compareTo)
  {
      if (target == null &amp;&amp; compareTo == null)
          return true;
      if (target == null || compareTo == null)
          return false;
      if (target.Length != compareTo.Length)
          return false;
      if (object.ReferenceEquals(target, compareTo))
          return true;
      if (!target.CanRead || !target.CanSeek)
          throw new ArgumentOutOfRangeException("target");
      if (!compareTo.CanRead || !compareTo.CanSeek)
          throw new ArgumentOutOfRangeException("target");
      lock (target)
      {
          lock (compareTo)
          {
              var origa = target.Position;
              var origb = compareTo.Position;
              try
              {
                  target.Position = compareTo.Position = 0;
  
                  // Shrink the number of comparisons.
                  var arr1 = new byte[4096];
                  var convert1 = new Converter() { Byte = arr1 };
                  var arr2 = new byte[4096];
                  var convert2 = new Converter() { Byte = arr2 };
  
                  int len;
                  while ((len = target.Read(arr1, 0, 4096)) != 0)
                  {
                      if (compareTo.Read(arr2, 0, 4096) != len)
                          return false;
                      for (var i = 0; i &lt; (len / 8) + 1; i++)
                          if (convert1.UInt64[i] != convert2.UInt64[i])
                              return false;
                  }
  
                  return true;
              }
              finally
              {
                  target.Position = origa;
                  compareTo.Position = origb;
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem is that the <code>convert1.UInt64[i] != convert2.UInt64[i]</code> <code>if</code> block (returning <code>false</code>) is being evaluated even when the values are the equal. I checked each on individually, then checked the outcome of the 'not equals'. <em>I am in pure disbelief</em>:</p>
  
  <p><img src="https://i.stack.imgur.com/c8yLH.png" alt="Values are not equal"></p>
  
  <p>I have not messed with the instruction pointer - this is how the code executed and the watch pin is live.</p>
  
  <p>Any ideas how this could happen?</p>