oracle PL/SQL 中的移位运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/776355/
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
Shift operators in PL/SQL
提问by drnk
Whether there is an alternative of shift
operators in PL/SQL? There is bitand
function, but it accepts only binary_integer-type arguments.
shift
PL/SQL 中是否有运算符的替代方案?有bitand
函数,但它只接受binary_integer类型的参数。
What should I do if I need check up lower/higher bit of really long number (probably set in the line)?
如果我需要检查非常长的数字的较低/较高位(可能在行中设置),我该怎么办?
In C
there are <<
and >>
operators. How I can realise them in PL/SQL?
在C
那里有<<
和>>
运营商。我如何在 PL/SQL 中实现它们?
采纳答案by drnk
Here is my own LPAD/RPAD solution.
这是我自己的 LPAD/RPAD 解决方案。
I take Tom Kyte packageas base and expand it.
我以Tom Kyte 包为基础进行扩展。
create or replace function bin_shift_right
( p_bin in varchar2,
p_shift in number default null) return varchar2
is
l_len number;
l_shift number;
begin
l_shift := nvl(p_shift, 1);
l_len := length(p_bin);
if (l_len <= 0) then
return null;
end if;
if (l_shift > l_len) then
l_shift := l_len;
end if;
return lpad(substr(p_bin, 1, l_len - l_shift), l_len, '0');
end bin_shift_right;
create or replace function shright
( p_num in number,
p_shift in number default null) return number
is
begin
if (trunc(p_num) <> p_num OR p_num < 0) then
raise PROGRAM_ERROR;
end if;
return nvl(to_dec(bin_shift_right(to_bin(p_num), p_shift), 2), 0);
end shright;
/
And tests
和测试
SQL>
SQL> select shright(123) from dual;
SHRIGHT(123)
------------
61
SQL>
SQL> select shright(123, 2) from dual;
SHRIGHT(123,2)
--------------
30
SQL>
SQL> select shright(123, 10) from dual;
SHRIGHT(123,10)
---------------
SQL> /
回答by Tim Lowes
The following answer is not endianness agnostic and my wording is based on little endian format...
以下答案与字节序无关,我的措辞基于小字节序格式...
You can shift bits simply multiplying (shift left) or dividing (shift right) the argument by 2 to the power of x where x is the number of bits to shift. for example, if I need to shift the low-order byte of a number (255:11111111) 16 bits to the left I would perform the following operation:
您可以简单地将参数乘以(左移)或除(右移)参数除以 2 的 x 次方,其中 x 是要移位的位数。例如,如果我需要将数字 (255:11111111) 的低位字节向左移动 16 位,我将执行以下操作:
select 255 * power(2,16) from dual;
-- the result will be (16711680:111111110000000000000000)
conversely, if I want to shift the value 16711680 16 bits to the right I would perform the following:
相反,如果我想将值 16711680 右移 16 位,我将执行以下操作:
select 16711680 / power(2,16) from dual;
-- the result will be (255:11111111)
回答by Oliver Michels
Since Oracle Version 8 it's possible you use java code in the database. In PL/SQL you can define a wrapper for the java code. e.g.
从 Oracle 版本 8 开始,您可以在数据库中使用 java 代码。在 PL/SQL 中,您可以为 java 代码定义一个包装器。例如
PACKAGE BODY JAVA_CODE
IS
function bitshift_left(x in number,
n in number) return number
is language java name 'com.foo.Bitshift(java.lang.Integer,
java.lang.Integer) return java.lang.Integer';
END JAVA_CODE;
In the java code you can then use the shift operator. Although a bit clumsy, but it can work this way.
在 java 代码中,您可以使用移位运算符。虽然有点笨拙,但它可以这样工作。
Sadly, this is not possible with Oracle XE, as there is no support for Java in that 'free' edition.
遗憾的是,这在 Oracle XE 中是不可能的,因为该“免费”版本不支持 Java。