?
Solved

Why are bitwise operands useful?

Posted on 2007-11-24
7
Medium Priority
?
1,035 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 1000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 1000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

762 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