Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    3

    Question Unanswered: Explain: if @code & power(2, @i) >0

    Hello,

    Can someone please help me understand this line "if @code & power(2, @i) >0 " in the following code. I know WHAT it does....I am looking for the logic HOW it does.

    For example:
    if I give @code = 30 it returns TRUE for @i=2
    but
    if I give @code = 17 it returns FALSE for @i=2

    How does it know.....I mean what's the logic behind.

    Someone please help me understand.

    thanks,


    =====================================


    set nocount on
    declare @code int, @i int
    select @code =17, @i = 0

    select ' ' as Result, ' ' as Code, ' ' as i , ' ' as powers

    while (@i <= 30)
    begin
    if @code & power(2, @i) >0
    begin
    select 'TRUE', @code , @i, power(2,@i)
    end
    else
    begin
    select 'FALSE', @code , @i, power(2,@i)
    end
    select @i = @i +1
    end

    =====================================

  2. #2
    Join Date
    Nov 2002
    Location
    Switzerland
    Posts
    524
    @code is a bitmap, 2 is the mask.
    The & applies a bitmap mask.

    E.g.

    30 = 11110
    17 = 10001
    2 = 00010

    => 30 & 2 => 1 is appearing in the 2nd position => TRUE
    => 17 & 2 => 1 is appearing isn't the 2nd position => FALSE

    is it clear now ?

  3. #3
    Join Date
    Sep 2003
    Posts
    3

    Thumbs up

    fadace ,

    That's exactly I was looking for.

    It helped me a lot. THANKS[]

    Originally posted by fadace
    @code is a bitmap, 2 is the mask.
    The & applies a bitmap mask.

    E.g.

    30 = 11110
    17 = 10001
    2 = 00010

    => 30 & 2 => 1 is appearing in the 2nd position => TRUE
    => 17 & 2 => 1 is appearing isn't the 2nd position => FALSE

    is it clear now ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •