CREATE FUNCTION [dbo].[BitMove](@long bigint,@operator varchar(3),@len int)
RETURNS bigint AS
BEGIN
if @len=0
return @long
declare @i bigint
declare @j bigint
set @i=cast(0x8000000000000000 as bigint)
set @j=cast(0x4000000000000000 as bigint)
if @operator='<<'
begin
while @len>0
begin
set @len=@len-1
if (@i&@long)=@i
set @long=@long^@i
if (@j&@long)=@j
begin
set @long=@long^@j
set @long=@long*2
if @len=0
set @long=@long|@i
end
else
set @long=@long*2
end
end
else
begin
set @len=@len-1
set @long=@long/2
if @operator='>>>' and (@i&@long)=@i
set @long=@long^@i
set @long=@long/power(2,@len)
end
return @long
END
RETURNS bigint AS
BEGIN
if @len=0
return @long
declare @i bigint
declare @j bigint
set @i=cast(0x8000000000000000 as bigint)
set @j=cast(0x4000000000000000 as bigint)
if @operator='<<'
begin
while @len>0
begin
set @len=@len-1
if (@i&@long)=@i
set @long=@long^@i
if (@j&@long)=@j
begin
set @long=@long^@j
set @long=@long*2
if @len=0
set @long=@long|@i
end
else
set @long=@long*2
end
end
else
begin
set @len=@len-1
set @long=@long/2
if @operator='>>>' and (@i&@long)=@i
set @long=@long^@i
set @long=@long/power(2,@len)
end
return @long
END