The information in this article applies to:
SYMPTOMSThe Microsoft ODBC Driver for Access and the Microsoft OLE DB Provider for Jet do not provide support for bit-wise operations in SQL statements. Attempts to use AND, OR, and XOR with numeric fields in a SQL statement return the result of a logical operation (true or false). CAUSEThe Microsoft Jet database engine does not support bit-wise operations in SQL. RESOLUTIONBit-wise operations must be replaced with the equivalent mathematical expressions or performed on the data outside of a SQL statement (performed in Visual Basic for Applications code for example). STATUSThis behavior is by design. MORE INFORMATIONYou can test for a single bit set in a long integer or integer field using the following algorithm: ( <value> \ (2^<bit>) ) mod 2This expression will return 1 if the <bit> is set and 0 if <bit> is not set. <bit> is numbered from 0 to 30 inclusive where <bit>=0 is the first bit. <bit> values greater than 30 will not work with this algorithm because Jet uses unsigned long integer values and 2^31 is one larger than the largest unsigned long integer value and thus will cause numeric overflow when the division is evaluated. Note that the \ operator and not the / operator is used. The \ operator is used for integer division. The / operator is used for floating point division and will cause unexpected results when used with this algorithm. You can check <bit> 31 for a long integer using the following algorithm: iif( <value> < 0, 1, 0 )This works because an unsigned long integer that is less than zero means the highest order bit (bit 31 for a long) is set. Suppose you have a table named Test and a long integer field named TestFlags. You can use the following SQL statements to test to see if the bit 11 is set in the TestFlags field
or replace 2^11 with 2048 to save some query calculation time:
You can use the following SQL to test for bit 31
but this SQL statement would be a much more efficient test for bit 31:
Note that you can also create calculated columns in SQL to display the
results of one or more bit checks:
You can run the following ADO code to verify that this algorithm works
correctly over various ranges and with various bit flags. Note the test
requires a blank Microsoft Access database named C:\Db1.mdb and a reference
to Microsoft ActiveX Data Objects.
Additional query words: kbDSupport
Keywords : |
Last Reviewed: November 18, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |