Andy Green
asked on
Bit mask error codes
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
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
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'
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'
ASKER
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,5 12,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
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,5
I cant test for every posible combination, how do I take the decimal, convert to binary and map to the error codes.
Andy
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.
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.
ASKER
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
I'll take a look over the weekend. Friday at 5 club now (well I'm late) and that means beer.
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks - got it now. It was the conversion into binary that stumped me and it does it for you implicitly.
Andy
Andy
>>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.
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.
"decimal" did not refer to a data type, but rather to "base 10".
Ah, good point. I should read more carefully.
Open in new window