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

发布于 2025-01-01 09:41:19 字数 345 浏览 4 评论 0 原文

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

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

这将:

  • 返回 Column1 没有设置第 4 位的行,例如 0, 1, 2, ..., 7(所有第 4 位都清零)和 16(即 00010000b)
  • 排除 Column1 为 8、9、 10、...、15 等

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

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

This would:

  • 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: are bitwise operators different from boolean operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞陪衬 2025-01-08 09:41:19

您可以:

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

编辑:(其中 b 是要测试的特定位)

OR 在 Access 中不可用,除非您设置 ANSI 模式。

you could:

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

edit: (where b is the specific bit to test)

OR is not available in Access unless you set ANSI Mode.

寂寞清仓 2025-01-08 09:41:19

如果您可以在 ANSI-92 查询模式(例如,通过更改 Access UI 查询模式或使用 ADO classic 或 ADO.NET 连接到它),使用 BAND 运算符。

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

8 AND 7: -1 
8 BAND 7: 0 

第一种情况 (AND) 将两个数字视为 True 值,因此 True AND True 给出 -1 (True)。我认为 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

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 

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
街角卖回忆 2025-01-08 09:41:19

在 VBA 中,您可以将布尔运算符应用于数字以执行按位运算

(13 AND 8) = 0

,但是在 SQL 中,这不起作用。但是,您可以编写一个在查询中调用的 VBA 函数。

In VBA, you can apply the Boolean operators to numbers in order to perform bitwise operations

(13 AND 8) = 0

In SQL, however, this does not work. However, you could write a VBA function that you call inside a query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文