Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Bit Manipulation Functions (4)

By ofer bester,

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

Total article views: 1043 | Views in the last 30 days: 2
 
Related Articles
FORUM

Help with converting varchar to datetime

Convert varchar to datetime

FORUM

converting varchar to numeric

converting varchar to numeric

FORUM

convert varchar to numeric

convert varchar to numeric

FORUM

Convert varchar to numeric

Please guide in Convert varchar to numeric

FORUM

Converting Getdate() To Varchar

Converting Getdate() to Varchar (16) to include Milliseconds

Tags
t-sql    
 
Contribute