PL/SQL中的移位运算符 [英] Shift operators in PL/SQL

查看:131
本文介绍了PL/SQL中的移位运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PL/SQL中是否有shift个运算符的替代方法?有bitand函数,但仅接受 binary_integer 类型的参数.

Whether there is an alternative of shift operators in PL/SQL? There is bitand function, but it accepts only binary_integer-type arguments.

如果我需要检查真的很长的数字的较低/较高位(可能在行中设置),该怎么办?

What should I do if I need check up lower/higher bit of really long number (probably set in the line)?

C中有<<>>个运算符.如何在PL/SQL中实现它们?

In C there are << and >> operators. How I can realise them in PL/SQL?

推荐答案

这是我自己的LPAD/RPAD解决方案.

Here is my own LPAD/RPAD solution.

我将 Tom Kyte软件包作为基础并进行扩展.

I take Tom Kyte package as base and expand it.

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> /

这篇关于PL/SQL中的移位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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