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