Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bit mask error codes

Posted on 2009-05-15
10
Medium Priority
?
436 Views
Last Modified: 2013-11-07
Hi All

We have an application that returns error code data as bit mapped.

ie
1 - error 1
2 - error 2
4 - error 3
8 - error 4
16 error 5

etc

If the app returne either a 1,2,4,8... I kow the error, but more oft than not it is a combination of errors ie

9 - error 1 and error 4.

Does anyone have an algorithm to convert these decimals back ito the error codes. I have a list of codes.

Ideally I'd like to do this on SQL Server, but code behind would be ok.

Andy
0
Comment
Question by:Andy Green
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24396504
You need BITWISE operators
declare @ERROR_1 int
declare @ERROR_2 int
declare @ERROR_3 int
declare @ERROR_4 int
 
set @ERROR_1 = 1
set @ERROR_2 = 2
set @ERROR_3 = 4
set @ERROR_4 = 8
 
declare @err int
set @err = @ERROR_1 | @ERROR_3
print '@err = ' + convert(varchar, @err)
 
if (@err & @ERROR_1 = @ERROR_1)
  print 'ERROR 1'
if (@err & @ERROR_2= @ERROR_2)
  print 'ERROR 2'
if (@err & @ERROR_3 = @ERROR_3)
  print 'ERROR 3'
if (@err & @ERROR_4 = @ERROR_4)
  print 'ERROR 4'

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24396617
You need to use a logical AND operation

DECLARE @Error int
SELECT @Error = 9
SELECT CASE WHEN @Error&1 =0 THEN 'False' ELSE 'True' END as 'Error 1',
CASE WHEN @Error&2 =0 THEN 'False' ELSE 'True' END as 'Error 2',
CASE WHEN @Error&4 =0 THEN 'False' ELSE 'True' END as 'Error 3',
CASE WHEN @Error&8 =0 THEN 'False' ELSE 'True' END as 'Error 4'
0
 
LVL 3

Author Comment

by:Andy Green
ID: 24397769
Not sure I understand your solution, or maybe it's my descrition of the problem.
I get a decimal value from the external app, which when converted to binary, equates to an error code, where ever there ia a 1.
The posible values go up to 1024. ie
1,2,4,8,16,32,64,128,256,512,1024 11 possible errors.
I cant test for every posible combination, how do I take the decimal, convert to binary and map to the error codes.
Andy
0
Independent Software Vendors: 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!

 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24397816
Why wouldn't you test for every condition? You have to, in order to map to the error codes, right?
You do a bit-wise AND against a test code and if the result = the test code, then that error code is contained in the map.
0
 
LVL 3

Author Comment

by:Andy Green
ID: 24397894
I see what you are saying, yes I agree I have to test aginst each of the 11, but I dont see how I get from the decimal to the actual bits, that represent the errors.
I'll take a look over the weekend. Friday at 5 club now  (well I'm late) and that means beer.
Andy
0
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 1500 total points
ID: 24411161
If you take a number, AND it with another number, it returns a result comprised of every BIT that was 1 in both numbers. Since you want to test for individual errors, you don't care what the "BITS" are. You simply need to see if the error code ANDed with a test error code is exactly equal to the test error code.
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 31581950
Thanks - got it now. It was the conversion into binary that stumped me and it does it for you implicitly.

Andy
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24414298
>>I dont see how I get from the decimal to the actual bits<<
You may not have any control over this, but there is no need to use decimals for this, an integer will suffice.  In fact if all you have are 11 variations then a smallint will do.
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24414344
"decimal" did not refer to a data type, but rather to "base 10".
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24414545
Ah, good point.  I should read more carefully.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

719 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