Technical Article

Convert Interger to Binary String for Bit Masks

,

The three functions here can be used to store Bit Masks as Integers.

These are functions to convert an integer into a binary string.
This can be useful if you store Bit Masks in your database as integers.

For example .. '00000000000011111001001001110011' to 1020531
Each Bit could represent an ON or OFF value. One integer column
could be equivalent to having 32 individual bit columns in your table.

There are three functions:

fn_GetBitString .. converts an integer to a bit string of a given size up to 32bit
fn_GetIntFromBitString .. converts a bit string to an integer
fn_GetBit .. get a specific bit value by bit location of an integer


Here are some example calls ..


DECLARE @I1 int, @I2 int


SELECT @I1 = dbo.fn_GetIntFromBitString('0101')

SELECT @I2 = dbo.fn_GetIntFromBitString('1001')


--Show Each Bit one at a time of the first integer

PRINT Cast(dbo.fn_GetBit(@I1,4) as char(1))

+Cast(dbo.fn_GetBit(@I1,3) as char(1))

+Cast(dbo.fn_GetBit(@I1,2) as char(1))

+Cast(dbo.fn_GetBit(@I1,1) as char(1))


--Do some Bitwise operations

PRINT dbo.fn_GetBitString( @I1 & @I2 ,4) --Bitwise AND

PRINT dbo.fn_GetBitString( @I1 | @I2 ,4) --Bitwise OR

PRINT dbo.fn_GetBitString( @I1 ^ @I2 ,4) --Bitwise XOR


-------------------------------------------------------------------
-- Turn an Integer into a base two Binary String
-- By: Robert Rinchuse Jr
--
-- PRINT dbo.fn_GetBitString(123456,32)
-------------------------------------------------------------------
CREATE FUNCTION dbo.fn_GetBitString
(
  @IntValue Int
, @BitSize tinyInt --1 to 32
)
RETURNS varchar(32)
AS
BEGIN
DECLARE @BitNum tinyint, @BitString varchar(32)

IF @BitSize>32 SET @BitSize=32

SELECT @BitNum=1, @BitString=''

WHILE @BitNum<=@BitSize
Begin
SELECT @BitString=
Cast( (convert(bigint,(@IntValue/power(cast(2 as bigint),@BitNum-1))) % 2)  as char(1))
-- Cast( dbo.fn_GetBit(@IntValue ,@BitNum)  as char(1))
+@BitString
SELECT @BitNum=@BitNum+1
End 

RETURN @BitString

END
GO

-------------------------------------------------------------------
-- Get a specific Bit value from and Integer
-- Useful if you are saving a Bit Mask as an integer
-- By: Robert Rinchuse Jr
--
-- PRINT dbo.fn_GetBit(123456,7)
-------------------------------------------------------------------
CREATE FUNCTION dbo.fn_GetBit
(
  @IntValue Int
, @BitNum tinyInt --1 to 32
)
RETURNS bit
AS
BEGIN

RETURN Cast( (convert(bigint,(@IntValue/power(cast(2 as bigint),@BitNum-1))) % 2)  as bit)

END
GO

-------------------------------------------------------------------
-- Turn a base two Binary String into an integer
-- By: Robert Rinchuse Jr
--
-- PRINT dbo.fn_GetIntFromBitString('11111001001001110011')
-------------------------------------------------------------------
-- PRINT dbo.fn_GetBitString(1020531,32)
-- PRINT dbo.fn_GetIntFromBitString(dbo.fn_GetBitString(1020531,32))
-- PRINT dbo.fn_GetBitString(dbo.fn_GetIntFromBitString('11111001001001110011'),32)
-------------------------------------------------------------------
CREATE FUNCTION dbo.fn_GetIntFromBitString
(
  @BitString varchar(32)
)
RETURNS Int
AS
BEGIN

DECLARE @BitNum tinyint, @IntValue int, @BitSize tinyint
SELECT @BitNum=1, @IntValue=0, @BitSize=LEN(IsNull(@BitString,''))

SELECT @BitString = REVERSE( @BitString )

WHILE @BitNum<=@BitSize
Begin
SELECT @IntValue=@IntValue+
Cast(SUBSTRING(@BitString,@BitNum,1) as BigInt)*POWER(cast(2 as bigint),@BitNum-1)

SELECT @BitNum=@BitNum+1
End 

RETURN @IntValue

END

GO

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating