create function dbo.solveBitVal(@val bigint)
returns @result table(id int, bitvalue bigint)
begin
declare @bitTranslate table (id int, bitvalue bigint)
declare @numbah table (num int)
declare @loopah int
set @loopah = 1
while @loopah <= 63
begin
insert into @bitTranslate (id, bitvalue) values (@loopah, POWER(convert(bigint,2),@loopah-1))
insert into @numbah (num) values (@loopah)
set @loopah = @loopah + 1
end
while @val > 0
begin
insert into @result
select max(id), max(bitvalue) from @bitTranslate where bitvalue <= @val
select @val = @val - max(bitvalue) from @bitTranslate where bitvalue <= @val
end
return
end
select * from dbo.solveBitVal(1337)
This function should do the trick.
No comments:
Post a Comment