title: Bit Operation For Finding String Difference
body: <p>The following string of mine tried to find difference between two strings.
  But it's horribly slow as it iterate the length of string:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  
  int hd(string s1, string s2) {
      // hd stands for "Hamming Distance"
      int dif = 0;
  
      for (unsigned i = 0; i &lt; s1.size(); i++ ) {
          string b1 = s1.substr(i,1);
          string b2 = s2.substr(i,1);
  
          if (b1 != b2) {
              dif++;
          }
      }  
  
      return dif;
  }
  
  int main() {
  
      string string1 = "AAAAA";
      string string2 = "ATATT";
      string string3 = "AAAAA";
  
      int theHD12 = hd(string1,string2);
      cout &lt;&lt; theHD12 &lt;&lt; endl;
  
      int theHD13 = hd(string1,string3);
      cout &lt;&lt; theHD13 &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Is there a fast alternative to do that?
  In Perl we can have the following approach:</p>
  
  <pre><code>sub hd {
      return ($_[0] ^ $_[1]) =~ tr/\001-\255//;
  }
  </code></pre>
  
  <p>which is much2 faster than iterating the position.</p>
  
  <p>I wonder what's the equivalent of it in C++?</p>