Bit wise

1. Senior Member
Join Date
Dec 1969
Posts
121

## Bit wise

Hi Guys...<BR><BR>just wondering if there is a standard way of working backwards for a bitwise number<BR><BR>I have records in a table and i am validating this table with a few simple rules (say 4 rules)<BR>hence <BR>rule 1 has bitwise value of 1<BR>rule 2 has bitwise value of 2<BR>rule 3 has bitwise value of 4<BR>rule 4 has bitwise value of 8<BR><BR>so if a record fails rule 2 and 4 its ErrorCode a value = 10 <BR><BR>if a record fails rule 1 and 3 its ErrorCode value = 5<BR><BR>My problem is i need a way of finding out from the ErrorCode which tests it has failed.<BR><BR>(my boss will give me the error code 10 ans ask which test it failed) <BR><BR>My finished system will have about 30 tests hence the numbers are going to be quite large....<BR><BR>any help would be great!<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## What database? What language?

And you better not have more than 31 tests or you can&#039;t do it in a single error code value. (Well, you can do 32, but it&#039;s a pain to do so. And you probably could go to 63 or 64 with SQL Server 2005.)<BR><BR>If you need to do the decoding in SQL, it&#039;s a bit of a pain in the neck. <BR><BR>But if you can do it in VBScript or VB.NET or C# or even JavaScript, it&#039;s pretty easy.<BR><BR>Example in VBS:<BR><BR>&#060;%<BR>errorCode = RS("errorCode")<BR><BR>bit = 1<BR>For rulenum = 1 To 31<BR> If ( errorCode AND bit ) &#060;&#062; 0 Then<BR> Response.Write "Failed rule #" & rulenum & "&#060;br/&#062;"<BR> End If<BR> bit = bit + bit<BR>Next<BR>%&#062;<BR><BR>And of course you could have done<BR><BR>&#060;%<BR>rules = Array( "--dummy--", _<BR> "Failed to put oil in the engine", _<BR> "Ran out of gas", _<BR> ...<BR> )<BR>%&#062;<BR>And then do<BR> Response.Write "Failed rule #" & rulenum & ": " & rules(ruleNum) & "&#060;br/&#062;"<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Maybe easy in DB after all!

Say you have a RULES table:<BR> rulenum - bitvalue - rule<BR> 1 - 1 - &#039;i before e&#039;<BR> 2 - 2 - &#039;except after c&#039;<BR> 3 - 4 - &#039;except when pronounced "a"&#039;<BR> ...<BR><BR>And then you just do:<BR><BR> SELECT rulenum, rule <BR> FROM rules, maintable<BR> WHERE maintable.someField = someValue<BR> AND ( maintable.errorCode AND rules.bitvalue ) &#060;&#062; 0<BR><BR>Change the AND to an & operator for SQL Server and MySQL.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•