# Thread: Explain: if @code & power(2, @i) >0

1. Registered User
Join Date
Sep 2003
Posts
3

## 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.

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. Registered User
Join Date
Nov 2002
Location
Switzerland
Posts
530
@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. Registered User
Join Date
Sep 2003
Posts
3

That's exactly I was looking for.

It helped me a lot. THANKS[]

@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
•