0

How did this function compare bits without loop? Please describe how it done. And what are this numbers mean: 6148914691236517205uL, 3689348814741910323uL, 1085102592571150095uL, 72340172838076673uL.

public static int HashDistance(ulong hash1, ulong hash2)
{
    ulong num = hash1 ^ hash2;
    num -= (num >> 1 & 6148914691236517205uL);
    num = (num & 3689348814741910323uL) + (num >> 2 & 3689348814741910323uL);
    num = (num + (num >> 4) & 1085102592571150095uL);
    return Convert.ToInt32(num * 72340172838076673uL >> 56);
}
pmi46b
  • 9
  • 3
  • Tip: look at the literal numbers in Base 2. – Dai Sep 23 '17 at 03:43
  • 1
    This function does not"compare" anything anywhere... Can you please clarify what you have problems understanding in that code? – Alexei Levenkov Sep 23 '17 at 04:06
  • @AlexeiLevenkov [this](https://stackoverflow.com/questions/29087434/c-sharp-fast-way-to-compare-2-integers-bit-by-bit-and-output-for-more-than-o) and [this](https://stackoverflow.com/questions/40676129/fastest-way-to-calculate-hamming-distance-in-c-sharp) both use loop to compare bits. I want to know how this done without loop. – pmi46b Sep 23 '17 at 04:21
  • There a lot of code that use loops... and a lot of code that does not. It does not mean that code without loops is wrong in some way. Presumably you understand [numeric literals](https://msdn.microsoft.com/en-us/library/aa664674(v=vs.71).aspx) and [bitwise operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/#logical-and-operator) - it is still unclear what exactly you need help with. – Alexei Levenkov Sep 24 '17 at 18:48
  • @AlexeiLevenkov I know this function calculate the [Hamming distance](http://www.csharpstar.com/csharp-string-distance-algorithm/). But how? – pmi46b Sep 26 '17 at 11:36

0 Answers0