oracle bitand函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10614637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:49:26  来源:igfitidea点击:

oracle bitand function

sqloracle

提问by osmanraifgunes

I am confused about the oracle bitand function. I know it is used to c?ntrol whether two bits are set. But, what is the meaning of being set. When and why it is used. If you can gve an example based on a real example, it will be so pleasent for me. Thank for your answers.

我对 oracle bitand 功能感到困惑。我知道它用于控制是否设置了两位。但是,设置是什么意思。何时以及为何使用它。如果你能给出一个基于真实例子的例子,那对我来说会很高兴。感谢您的回答。

回答by Bob Jarvis - Reinstate Monica

In binary, "set" means "has the value 1". "Not set" means "has the value 0".

在二进制中,“set”的意思是“值为 1”。“未设置”表示“值为 0”。

From the Oracle docs for BITAND:

来自 BITAND 的 Oracle 文档:

"The result is computed in several steps. First, each argument A is replaced with the value SIGN(A)*FLOOR(ABS(A)). This conversion has the effect of truncating each argument towards zero. Next, each argument A (which must now be an integer value) is converted to an n-bit two's complement binary integer value. The two bit values are combined using a bitwise AND operation. Finally, the resulting n-bit two's complement value is converted back to NUMBER."

“结果分几个步骤计算。首先,每个参数 A 被替换为值 SIGN(A)*FLOOR(ABS(A))。这种转换具有将每个参数截断为零的效果。接下来,每个参数 A (它现在必须是一个整数值)被转换为一个 n 位二进制补码二进制整数值。这两个位值使用按位 AND 运算组合。最后,得到的 n 位二进制补码值被转换回 NUMBER。”

Put simply, this function truncates its arguments, converts them to a binary number (currently limited to 128 bits), AND's the two binary numbers together, and returns the result of converting the binary number back to a NUMBER.

简单地说,这个函数截断它的参数,将它们转换为一个二进制数(目前限制为 128 位),将两个二进制数进行 AND 运算,并返回将二进制数转换回一个 NUMBER 的结果。

Here's the result of all possible combinations of zero and one:

这是所有可能的零和一组合的结果:

SELECT BITAND(0, 0) AS "0, 0",  -- i.e. 0 AND 0 = 0
       BITAND(0, 1) AS "0, 1",  -- i.e. 0 AND 1 = 0
       BITAND(1, 0) AS "1, 0",  -- i.e. 1 AND 0 = 0
       BITAND(1, 1) AS "1, 1"   -- i.e. 1 AND 1 = 1
  FROM DUAL;

A more complex example would be ANDing together 11 and 5. In binary, 11 decimal becomes "1011". 5 decimal becomes "0101" binary. If you AND these values together, as in

一个更复杂的例子是将 11 和 5 进行 AND 运算。在二进制中,十进制的 11 变为“1011”。5个十进制变成“0101”二进制。如果您将这些值 AND 在一起,如

1 0 1 1
0 1 0 1
-------
0 0 0 1

you get 1 binary, which is still 1 when converted back to decimal.

你得到 1 个二进制,当转换回十进制时它仍然是 1。

Share and enjoy.

分享和享受。