Solved

Why are bitwise operands useful?

Posted on 2007-11-24
7
1,032 Views
Last Modified: 2010-04-21
I noticed that MSSQL provides quite extensive support for bitwise operands (& | ~ ^ ), and I'm wondering why on earth they might be useful.  Does anyone have any good uses for these in practice?
0
Comment
Question by:PaultheBroker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20343542
when you have a field where you store bitwise information, like on/off flags.

the databases view is a good example, it's status field actually contains many different status flags.
using bitwise operations, you can easily extract a single bit flag from it.
0
 
LVL 6

Author Comment

by:PaultheBroker
ID: 20343607
Hi Angel !  - OK - I know I said I was 'Advanced' but can you give an example of the above?  :)  I'm not sure what you mean by the 'databases view' and don't get how using these bitwise operands will help....
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 20343658
ok, here we go:
http://technet.microsoft.com/en-us/library/ms179900.aspx

look at the explanation of the status, status2 and category fields.
you see the (decimal) values, which are however added up as bit values:

1 = 0000000000001
2 = 0000000000010
4 = 0000000000100
8 = 0000000001000
etc.

so, adding up 2 status values (by bitwise OR), you would get for example 1+2=
3 = 0000000000011

now, to see afterwards if a certain bit value is set, you again use a bit mask operation (AND):
checking out if status=2 is "set", you do:
status & 2  
if that returns 2, it means that that bit is set.


0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 6

Author Comment

by:PaultheBroker
ID: 20343752
Great !  - actually, just found another example in the BOL (which thanks to your clear explanation above I now understand...)

IF @@OPTIONS & 512 > 0
   RAISERROR ('Current user has SET NOCOUNT turned on.',1,1)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20343755
yes, exactly
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20380083
http://Q_22992466.html
SELECT    SUM(value) as "UP", SUM(value^1) as "DOWN"
FROM            ItemVote
WHERE           itemID = '#attributes.itemID#'

quick way to select both sides of a coin (bit)
btw, the #s are from CF
0
 
LVL 6

Author Closing Comment

by:PaultheBroker
ID: 31410786
Thanks to both of you...I'm sure there are more out there, but a week is about as long as I think reasonable to hold this question open.  Maybe other uses will occur in due time !!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question