Hello all,

I have little experience with how the ampersand works in the context of SQL Server so I'm hoping someone can help me in that regard. I have a stored procedure that returns a bit indicator for a status. Within the sproc I have the following code:

The other variables are parameters fed into the sproc. The first two conditions of the IIF statement are straight forward but the last one involving @MyAccountStatus (this is an int) and @TeamEventEnabled puzzles me. What exactly is it evaluating?

Thanks in advance,

Glen

I have little experience with how the ampersand works in the context of SQL Server so I'm hoping someone can help me in that regard. I have a stored procedure that returns a bit indicator for a status. Within the sproc I have the following code:

```
declare @TeamEventEnabled int = 0x1000000;
iif(@EventStartDate > @NewTeamEventEffectiveDate AND @EventType = 0 AND @MyAccountStatus & @TeamEventEnabled > 0, 1, 0);
```

The other variables are parameters fed into the sproc. The first two conditions of the IIF statement are straight forward but the last one involving @MyAccountStatus (this is an int) and @TeamEventEnabled puzzles me. What exactly is it evaluating?

Thanks in advance,

Glen

It performs a bitwise logical AND operation between two integer values.

The & bitwise operator performs a bitwise logical AND between the two expressions, taking each corresponding bit for both expressions.

0000 0000 0000 0111 -- 7

0000 0000 0000 0010 -- 2

-------------------

0000 0000 0000 0010 -- Only the 2 bit is common in both 7 and 2

For each single digit if the value is 1 then we get 1 else 0

0000 0000 0000 0111

0000 0000 0000 0010

for 2nd position from right we have 1 & 1 , so if value are 1 & 1 then we get 1.

For other 1 & 0 , 0 & 0 , 0 & 1 --- we will get 0.

For 7 -- its internal binary representation is 0000 0000 0000 0111. Divide the number by 2 take reminder until your get 0.

7 / 2 ---- 1

3 / 2 ------- 1

1/ 2 ------- 1

All remaining will be zero since we have to make it 16 digits. <<16 digits Binary System>>

So 0000 0000 0000 0111

Source -

http://www.wikihow.com/Convert-from-Decimal-to-Binary#/Image:Convert-from-Decimal-to-Binary-Step-3-Version-4.jpg

Hope it helps

!!

It's more of a design decision that the application designers would have taken to accommodate multiple related bit fields into a single field/value so that rather than exchanging multiple values, you only need to work with one value.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Because an integer is actually a collection of bits (little 1s and 0s), This is typically used when you want to represent a combination of switches (1 = on, 0 = off) as a single integer. You can then use a bitwise AND to find out overlaps between two sets of switches.

Here's a small example that I could come up with:

Open in new window