简体   繁体   English

如何在MS-ACCESS中执行按位运算算法

[英]How to perform bitwise operations arithmetic in MS-ACCESS

Inside MSACCESS I want to use relatively simple bitwise operations in WHERE clause of queries such as this: 在MSACCESS内部,我想在查询的WHERE子句中使用相对简单的按位运算,例如:

SELECT *
FROM Table1
WHERE Column1 (some operator) 8 = 0

This would: 这个会:

  • Return rows where Column1 does not have its 4th bit set eg 0, 1, 2, ..., 7 (all have their 4th bit clear) and 16 (it is 00010000b) 返回没有设置Column1第4位的行,例如0、1、2,...,7(所有第4位都清零)和16(其为00010000b)
  • Exclude rows where Column1 is 8, 9, 10, ..., 15 etc. 排除Column1为8、9、10,...,15等的行。

PS: are bitwise operators different from boolean operations? PS:按位运算符与布尔运算不同吗?

you could: 你可以:

WHERE (((column\(2^b)) mod 2) = 1)

edit: (where b is the specific bit to test) 编辑:(其中b是要测试的特定位)

OR is not available in Access unless you set ANSI Mode. 除非您设置ANSI模式,否则Access中不提供OR。

If you can run your query in in ANSI-92 Query Mode (eg by changing the Access UI Query Mode or by connecting to it using ADO classic or ADO.NET), use the BAND operator. 如果您可以在ANSI-92查询模式下运行查询(例如,通过更改Access UI查询模式或通过使用ADO classic或ADO.NET进行连接),请使用BAND运算符。

The following code sample prints this to the Immediate window: 下面的代码示例将其打印到立即窗口:

8 AND 7: -1 
8 BAND 7: 0 

The first case (AND) treats both numbers as True values, so True AND True gives -1 (True). 第一种情况(AND)将两个数字都视为True值,因此True AND True给出-1(True)。 I think the BAND approach is what you're after. 我认为BAND方法是您所追求的。

Public Sub BitwiseAndQuery()
    'the db engine treats numbers as booleans with AND '
    Debug.Print "8 AND 7: "; _
        CurrentDb.OpenRecordset("SELECT 8 AND 7")(0)

    'ADO includes BAND for bitwise AND '
    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open "SELECT (8 BAND 7)", CurrentProject.Connection
    Debug.Print "8 BAND 7:"; rs(0)
    rs.Close
    Set rs = Nothing
End Sub

In VBA, you can apply the Boolean operators to numbers in order to perform bitwise operations 在VBA中,您可以将布尔运算符应用于数字,以便执行按位运算

(13 AND 8) = 0

In SQL, however, this does not work. 但是,在SQL中,这不起作用。 However, you could write a VBA function that you call inside a query. 但是,您可以编写在查询中调用的VBA函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM