Technical Article

Bit Manipulation Functions (4)

,

Execute / Usage example script:

declare @BitMap BIGINT
declare @bitNo TINYINT

set @BitMap = 0
set @bitNo = 55

print '- Bit # = '+CONVERT(VARCHAR,@bitNo)+ ', BitMap value = '+ CONVERT(VARCHAR,@BitMap)
print ' Check bit #'+CONVERT(VARCHAR,@bitNo)
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

set @BitMap = dbo.fnBitSetOn(@BitMap, @bitNo)
print ' Turn bit #'+CONVERT(VARCHAR,@bitNo) +' ON'+
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

set @BitMap = dbo.fnBitSetOff(@BitMap, @bitNo)
print ' Turn bit #'+ CONVERT(VARCHAR,@bitNo) +' OFF'
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

set @BitMap = dbo.fnBitToggle(@BitMap, @bitNo)
print ' Toggle bit #'+ CONVERT(VARCHAR,@bitNo)
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)
set @BitMap = dbo.fnBitToggle(@BitMap, @bitNo)
print ' Toggle bit #'+ CONVERT(VARCHAR,@bitNo)
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

print ''
set @BitMap = -1

print '- Bit # = '+CONVERT(VARCHAR,@bitNo)+ ', BitMap value = '+ CONVERT(VARCHAR,@BitMap)
set @BitMap = dbo.fnBitSetOn(@BitMap, @bitNo)
print ' Turn bit #'+CONVERT(VARCHAR,@bitNo) +' ON'+
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

set @BitMap = dbo.fnBitSetOff(@BitMap, @bitNo)
print ' Turn bit #'+ CONVERT(VARCHAR,@bitNo) +' OFF'
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

set @BitMap = dbo.fnBitToggle(@BitMap, @bitNo)
print ' Toggle bit #'+ CONVERT(VARCHAR,@bitNo)
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)
set @BitMap = dbo.fnBitToggle(@BitMap, @bitNo)
print ' Toggle bit #'+ CONVERT(VARCHAR,@bitNo)
+' - Is bit ON = '+ CONVERT(VARCHAR,dbo.fnBitIsOn(@BitMap, @bitNo))
+', Value ='+ CONVERT(VARCHAR,@BitMap)

 

Result:

- Bit # = 55, BitMap value = 0
Check bit #55 - Is bit ON = 0, Value =0
Turn bit #55 ON - Is bit ON = 1, Value =36028797018963968
Turn bit #55 OFF - Is bit ON = 0, Value =0
Toggle bit #55 - Is bit ON = 1, Value =36028797018963968
Toggle bit #55 - Is bit ON = 0, Value =0

- Bit # = 55, BitMap value = -1
Turn bit #55 ON - Is bit ON = 1, Value =-1
Turn bit #55 OFF - Is bit ON = 0, Value =-36028797018963969
Toggle bit #55 - Is bit ON = 1, Value =-1
Toggle bit #55 - Is bit ON = 0, Value =-36028797018963969

-----------------------------------------------------------------------------------
-- Check if bigint Bit is ON
-----------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnBitIsOn (@BitMap BIGINT, @BitNo TINYINT = 0) RETURNS TINYINT
-- v1.0.0 2006.09.26, Ofer Bester
AS BEGIN
IF (@BitNo > 63)
 RETURN 0
 
IF (@BitNo = 63) 
BEGIN
 IF (@BitMap >= 0)
 RETURN 0
 ELSE
 RETURN 1
END
 
IF ( @BitMap & POWER( CAST(2 AS BIGINT), @BitNo ) ) <> 0
RETURN 1

RETURN 0
END
GO

-----------------------------------------------------------------------------------
-- Set bigint Bit OFF
-----------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnBitSetOff (@BitMap BIGINT, @BitNo TINYINT = 0) RETURNS BIGINT
-- v1.0.0 2006.09.26, Ofer Bester
AS BEGIN
IF (@BitNo > 63)
RETURN @BitMap
 
IF (@BitNo = 63)
RETURN @BitMap & 0x7FFFFFFFFFFFFFFF
 
RETURN (@BitMap & ~POWER( CAST(2 AS BIGINT), @BitNo )) -- Set bit # @BitNo OFF
END
GO

-----------------------------------------------------------------------------------
-- Set bigint Bit ON
-----------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnBitSetOn (@BitMap BIGINT, @BitNo TINYINT = 0) RETURNS BIGINT
-- v1.0.0 2006.09.26, Ofer Bester
AS BEGIN
IF (@BitNo > 63)
RETURN @BitMap
 
IF (@BitNo = 63)
RETURN @BitMap | 0x8000000000000000
 
RETURN (@BitMap | POWER( CAST(2 AS BIGINT), @BitNo )) -- Set bit # @BitNo ON
END
GO

-----------------------------------------------------------------------------------
-- Toggle bigint Bit ON/OFF
-----------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnBitToggle (@BitMap BIGINT, @BitNo TINYINT = 0) RETURNS BIGINT
-- v1.0.0 2006.09.26, Ofer Bester
AS BEGIN
IF ( dbo.fnBitIsOn(@BitMap, @BitNo) = 1 )
RETURN dbo.fnBitSetOff(@BitMap, @BitNo)

RETURN dbo.fnBitSetOn(@BitMap, @BitNo)
END

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating