I'm looking for a fast way to calculate the hamming weight/population count/"the number of 1 bits" of a BINARY(1024) field. MySQL has a BIT_COUNT function that does something like that. I couldn't find a similar function in T-SQL?
Or would you suggest storing the binary data in a field of another type?
If you don't know what I'm talking about, here's a Wikipedia article about the hamming weight.
You could use a helper table with precalculated Hamming weights for small numbers, like bytes, then split the value accordingly, join to the helper table and get the sum of partial Hamming weights as the value's Hamming weight:
Didn't find anything specifically about hamming weight, but here's one for hamming distance:
This computes the hamming distance between two values. The hamming weight of a single value would be the hamming distance between that value and an array of zero-values.
I couldn't find a good way to do it. In the end I calculated the hamming weight in Java and periodically update the bit counts in the database.
When you are playing with smaller value (something like 16 bit max), The most efficient way to do it with SQL Server is using an Table with all result calculated and using a join.
I have speed up a query from 30 sec to 0 sec by doing this kind of thing on a query which should calculate Hamming Weight of a 4 bit value on 17'000 rows .
Of course it is an ugly solution and it probably won't suit well for long bit field.