Home Forums Programming General General UDFs to emulate BITTEST(), BITSET() ? RE: General UDFs to emulate BITTEST(), BITSET() ?

  • I'm playing around with your math versions, and I'm not getting quite the result I expected. Did I misinterpret what you had in mind?

    -----------

    CREATE FUNCTION dbo.BitClear(@nvalue BigInt, @nposition TinyInt) 

    RETURNS BigInt AS 

    BEGIN

    Declare @nResult Bigint

    set @nResult = @nValue  ^ Power( 2, @nPosition)

    Return @nResult

    END

    CREATE FUNCTION dbo.BitSet(@nvalue BigInt, @nposition TinyInt) 

    RETURNS BigInt AS 

    BEGIN

    Declare @nResult Bigint

    set @nResult = @nValue | Power( 2, (@nPosition))

    Return @nResult

    END

    -----------

    now when I try to use them, I get the expected results for positions 0 and 1, but both functions give me identical results for positions 3 +

     

    Declare @Cleared BigInt

    Declare @Set BigInt

    Declare @test-2 BigInt

    Set @test-2 = 67

    Set @cleared = dbo.BitClear(@test,3)

    Set @set = dbo.BitSet(@test,3)

    Select

    @test-2,@cleared,@set,

     fn_replinttobitstring(@test),

     fn_replinttobitstring(@cleared),

     fn_replinttobitstring(@set)