When you play with a lower value (something like a 16-bit max), the most efficient way to do this using SQL Server is to use a table with all the results calculated and using the join.
My query is accelerated from 30 seconds to 0 seconds, doing such things in a query that should calculate the Hamming weight of a 4-bit value for 17,000 rows.
WITH HammingWeightHelper AS (
SELECT x, Fx
FROM (VALUES(0,0),(1,1),(2,1),(3,2),
(4,1),(5,2),(6,2),(7,3),
(8,1),(9,2),(10,2),(11,3),
(12,2),(13,3),(14,3),(15,4)) AS HammingWeight(x, Fx)
)
SELECT HammingWeight.Fx As HammingWeight, SomeTable.Value As bitField
FROM SomeTable INNER JOIN
HammingWeightHelper ON HammingWeightHelper.x = SomeTable.Value
Of course, this is an ugly solution, and it is probably not well suited for a long bit field.