Bitwise operations in MS access

孤人 提交于 2019-12-22 08:54:17

问题


I have a table which contains an answer. This answer is a combination of a set list of numbers. (see below)

Possible values

2 4 8 16 32 64

If I have the answer of Answer 24 – I need some method to work out the only values that could have been selected are 16 and 8. I am told if this where SQL I would use a bitwise operation.

I am fairly advance in Access but tend not to use VBA code, but am open to all ideas.


回答1:


Compare KB194206 (the gist of it: "The Microsoft Jet database engine does not support bitwise operations in SQL. This behavior is by design."), which basically means you're stuck with VBA.

And bitwise in VBA is very simple, because in VBA all locical operators are bitwise.

Just create a wrapper function:

Function BitAnd(Value1 As Long, Value2 As Long) As Long
    BitAnd = Value1 And Value2
End Function

and use it in your SQL

WHERE
    BitAnd([someColumn], 64) > 0

Do the same thing for the other bitwise operations you need.


You could use it in joins, as well. Assume you have an FlagsTable (FlagValue, FlagName) that contains the names for each of your available flags:

SELECT
  d.Id,
  d.BitField,
  f.FlagName
FROM
  DataTable d
  INNER JOIN FlagsTable f ON BitAnd(d.BitField, o.FlagValue) > 0

This query would be one way to address your "24 consists of 16 and 8" sub-question.



来源:https://stackoverflow.com/questions/24143813/bitwise-operations-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!