如何在MS-ACCESS中执行按位运算算法 [英] How to perform bitwise operations arithmetic in MS-ACCESS

查看:168
本文介绍了如何在MS-ACCESS中执行按位运算算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSACCESS内部,我想在查询的WHERE子句中使用相对简单的按位运算,例如:

Inside MSACCESS I want to use relatively simple bitwise operations in WHERE clause of queries such as this:

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

这将:

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

PS:按位运算符与布尔运算符不同吗?

PS: are bitwise operators different from boolean operations?

推荐答案

如果可以在

If you can run your query in in ANSI-92 Query Mode (e.g. by changing the Access UI Query Mode or by connecting to it using ADO classic or ADO.NET), use the BAND operator.

以下代码示例将其打印到立即窗口:

The following code sample prints this to the Immediate window:

8 AND 7: -1 
8 BAND 7: 0 

第一种情况(AND)将两个数字都视为True值,因此True AND True给出-1(True).我认为BAND方法是您所追求的.

The first case (AND) treats both numbers as True values, so True AND True gives -1 (True). I think the BAND approach is what you're after.

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

这篇关于如何在MS-ACCESS中执行按位运算算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆