Solved

Why are bitwise operands useful?

Posted on 2007-11-24
7
1,025 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
  • 3
  • 3
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Author Comment

by:PaultheBroker
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
yes, exactly
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
Comment Utility
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
Comment Utility
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

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.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now