Here is some code that takes an int error code (scode) and tries to see if it fits a certain pattern. Should I be using the modulo division operator to be doing this?
const int MASK_SYNTAX_ERR = -2146827000;
if ((MASK_SYNTAX_ERR % scode) == MASK_SYNTAX_ERR)
scriptError.GetSourceLineText(out sourceLine);
Background: I had to deduce the value of MASK_SYNTAX_ERR through observation. Here are the various syntax error codes that I observed:
// -Int Value (Formatted Value "0x{0:X8}")
-2146827281 (0x800A03EF)
-2146827279 (0x800A03F1)
-2146827280 (0x800A03F0)
-2146827283 (0x800A03ED)
-2146827284 (0x800A03EC)
Here are a couple of logic error codes for comparison:
-2146823281 (0x800A138F)
-2146823279 (0x800A1391)
(Trivia: The code itself is calling IActiveScriptError.GetSourceLineText, this is from a IActiveScriptSite.OnScriptError implementation.)
The bitwise & operator is what you should use, like this:
const int MASK_SYNTAX_ERR = -2146827000;
if (MASK_SYNTAX_ERR & scode != 0)
scriptError.GetSourceLineText(out sourceLine);
More about what’s going on.
The binary representation of MASK_SYNTAX_ERR is
1000 0000 0000 1010 0000 0101 0000 1000This comparison will return true for any number that has 1’s in the same positions. Take your first syntax error code, for example:
So the mask works here. Now, comparing a logic error code:
It also works here, which it shouldn’t. It seems like maybe your deduced mask may be wrong because it is too complicated. Looking at your syntax codes and logic codes together:
It looks like the 13th bit from the left is the key difference between logic errors and syntax errors. So you could do something like this:
You can use a similar analysis to figure out masks for other purposes. A lot of times, a mask is used to grab a single bit from an int, rather than something complicated like you had. It could be that the 0x800A part of the code means a certain kind of error, and the last part gives information about the error. You’ll have to do some experimentation yourself, but hopefully this will get you on the right track for doing bitmasks.