Technical Article

Get the Value of a BitMask

,

This function will take a 64 bitmask string and return the value of bitmask. If you need 128, 256, etc simply change the length of the parameter, being careful to remember you may need to change the return type of int to BigInt if you get too long.

create function dbo.fnBitMaskToInt
(
@Bits varchar(64)
)
returns int
as
begin
declare @Value int
declare @i int
declare @length int
declare @CurChar char
set @value = 0

set @length = len(@bits)
set @i = 0
while @i < @length
begin
set @curchar = left(@bits,1)
set @bits = right(@bits,len(@bits)-1)

if @curchar = '1'
begin
set @value = @value + power(2,@i)
end
set @i = @i + 1
end

return @value
end

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating