Solved

Why are bitwise operands useful?

Posted on 2007-11-24
7
1,028 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]
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 142

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 142

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

777 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