title: calculate hamming distance and weight in sqlite
body: <p>Is there a good way to calculate hamming distance and weight in sqlite? It supports bit-wise operators but I want to order results based on hamming weight, and there is no support for bitcount in sqlite.</p>
  
  <p>To be more elaborate, let's say I have those rows:
  <code>
  1011
  1000
  1100
  0011
  </code>
  and given the 1st row (1011) I would like to get as a result last row (0011) which has the most 1s if you AND them.</p>
  
  <p>In my case the numbers will be about 650 digits long, and I have about 3500 rows.</p>
  
  <p>I've found this solution which works with blobs of text, but I would like something more optimal:</p>
  
  <pre><code>create table ts (x blob); 
  insert into ts(x) values ('00010');
  ...
  select x &amp; '10011', length(replace( x &amp; '10011','0','')) as weight from ts;
  </code></pre>