Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why are bitwise operands useful?

Posted on 2007-11-24
7
Medium Priority
?
1,040 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

604 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