• I wrote a function that expands to 64 bits (8x8) that might be usefull...

    Create Function fn_BigIntToBinary (@bi BigInt) 

    RETURNS Varchar(72)

    AS

    Begin

    /*

    -- Returns 64 bits of passed BigInt, each byte separated by a space.

    Select dbo.fn_BigIntToBinary(123456789)

    */

     Declare @RetVal Varchar(72), @Hi16a BigInt, @Hi16b Bigint, @Low16a BigInt, @Low16b BigInt,

      @Hi32 BigInt, @RetVal1 VarChar(72)

     Select @Hi32 = @bi / Power(Convert(BigInt,2), 32)

     select  @Hi16a = (@bi / (65536 * 256)) & (Power(2, 16) - 1),

      @Hi16b = (@bi / 65536) & (Power(2, 16) - 1),

      @Low16a = (@bi / 256) & (Power(2, 16) - 1),

      @Low16b = @bi & (Power(2, 16) - 1),

      @RetVal1 = case 

        When @bi > Power(Convert(BigInt,2), 32) then dbo.fn_BigIntToBinary(@Hi32)

        Else '00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000'

          End

     Set @RetVal = Right(@RetVal1, 35)  + ' ' + 

      Right(fn_replinttobitstring(@Hi16a), 8) + ' ' + 

      Right(fn_replinttobitstring(@Hi16b), 8) + ' ' + 

      Right(fn_replinttobitstring(@Low16a), 8) + ' ' + 

      Right(fn_replinttobitstring(@Low16b), 8)

     Return @RetVal

    End

     

    Hope it cut&pastes ok....



    Once you understand the BITs, all the pieces come together