c# - T-SQL Hamming distance function capable of decimal / string / UInt64 -


I need to convert this C # function to T-SQL UDF

Let me all

return csharp function for this 2 hash while return of T-SQL function 52

P> 14714557628763197901

15383788748848265778

  public static ulong csharp_hamming_distance (ulone hash1, ulone hash2) {ulong x = hash1 ^ hash2; Consta Vaughan M1 = 0x5555555555555555UL; Constant Woolong M2 = 0x3333333333333333UL; Const ulong h01 = 0x0101010101010101UL; Ulhone M4 = 0x0f0f0f0f0f0f0f0fUL; X - = (X & gt; 1) & amp; M1; X = (x and amp; m2) + ((x> 2) & amp; m2); X = (x + (x >> 4)) & amp; M4; Return (x * h01)> & gt; 56; }   

I have a sample but it does not give me similar results.

, @ I = 1, @ lan = case when lane (@ value1) & gt; When the LAN (@Value2) returns the lane (@ value1) the other lane (@Value2) end if (@well1 is zero) or (@ value2 is empty) returns (@ and & lt; = @ LAN) Select @Distance = @Distance + when substring (@ value1, @ i, 1) = substrings (@ value2, @i, 1) then when substring based on 0 (@ value1, @ i, 1) & lt; Substring (@ value2, @ i, 1) then CAST (substring (@ value2, @ i, 1) in small form) - CAST (substring (@ value1, @ i, 1) in short) when substring (@ value1, @ I, 1) & gt; Substring (@ value2, @ i, 1) then small as CATT (substring (@ value1, ii, 1)) - CATT (substring (@ value2, @i, 1) in small form) and 1 End, @ E = @

Any assistance will be apreciated

In the Humming calculation, the integer is considered as bits. Heming is the number of distance bit differences, which can be calculated as the number of non-zero bits in the xorax of two values. To provide two integers, the bitwise critical distance is actually 40.

  14714557628763197901 = 1100110000110100100111000011001111001001011100011101000111001101 15383788748848265778 = 1101010101111110001100100101110000111010110000000111101000110010 ^ = 0001100101001010101011100110111111110011101100011010101111111111   

which is just a great way to 40 non-displayed zero bits C # count them.

This is not the case with wire. In TSQL, you are doing string hamming, which is classically classic hammanging on both the values ​​on which the characters are different gives:

  "14714557628763197901" "15383788748848265778" 01111111110111111111 = 18   

For example, the TSQL code is modifying the modified hammer; To get the classic hamming distance, just remove the previous two to sections.

Binary to prevent distance at bigint will be very difficult in TSQL because TSQL does not support bitware operation on the granite. However, you can calculate on different left and right parts using integer arithmetic, and then add them. The only difficult part is that the cursed MSB and the effect on the transfer.

Hammang's distance to the decimal is not well defined, you will need to be more specific about what you think.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -