% d=bdistance(dec1,dec2)
% Returns binary distance between decimal numbers dec1 and dec2.
% 
% George Ginis

function d = bdistance(dec1,dec2)
  
  % a table is defined for distances of decimals between 0 and 15
  dist_table=...
  [0     1     1     2     1     2     2     3     1     2     2 ...
   3     2     3     3     4;
   1     0     2     1     2     1     3     2     2     1     3 ...
   2     3     2     4     3;
   1     2     0     1     2     3     1     2     2     3     1 ...
   2     3     4     2     3;
   2     1     1     0     3     2     2     1     3     2     2 ...
   1     4     3     3     2;
   1     2     2     3     0     1     1     2     2     3     3 ...
   4     1     2     2     3;
   2     1     3     2     1     0     2     1     3     2     4 ...
   3     2     1     3     2;
   2     3     1     2     1     2     0     1     3     4     2 ...
   3     2     3     1     2;
   3     2     2     1     2     1     1     0     4     3     3 ...
   2     3     2     2     1;
   1     2     2     3     2     3     3     4     0     1     1 ...
   2     1     2     2     3; 
   2     1     3     2     3     2     4     3     1     0     2 ...
   1     2     1     3     2;
   2     3     1     2     3     4     2     3     1     2     0 ...
   1     2     3     1     2;
   3     2     2     1     4     3     3     2     2     1     1 ...
   0     3     2     2     1; 
   2     3     3     4     1     2     2     3     1     2     2 ...
   3     0     1     1     2; 
   3     2     4     3     2     1     3     2     2     1     3 ...
   2     1     0     2     1; 
   3     4     2     3     2     3     1     2     2     3     1 ...
   2     1     2     0     1; 
   4     3     3     2     3     2     2     1     3     2     2 ...
   1     2     1     1     0];
   
   
   
  d=dist_table(dec1+1,dec2+1);
  
  % alternatively, the following method can be used, 
  % but it is rather inefficient
  %xor_d = bitxor(dec1,dec2);   % compute binary xor
  %bin_d = dec2bin(xor_d);      % convert xor to binary representation
  %d = length (find(bin_d=='1'));



