Why not just write your own bit_count code in T-SQL? There is no need to use SQL CLR if you only need to count the number of bits in bigint. Here is an example:
CREATE FUNCTION bit_count ( @pX bigint ) RETURNS int AS BEGIN DECLARE @lRet integer SET @lRet = 0 WHILE (@pX != 0) BEGIN SET @lRet = @lRet + (@pX & 1) SET @pX = @pX / 2 END return @lRet END GO
Also here fiddle you can try to see this function in action.
It should be remembered that this algorithm only works on non-negative figures. If you are looking for an algorithm that works with negative icons, see this link .
source share