ITPub博客

首页 > 数据库 > Oracle > oracle 中的bitand bitor bitxor

oracle 中的bitand bitor bitxor

原创 Oracle 作者:coldwater 时间:2005-03-31 13:24:48 0 删除 编辑

参考:

http://blog.itpub.net/post/6/1609

http://asktom.oracle.com/pls/ask/f?p=4950:8:280872129227966139::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:375818206678,


ORACLE中的BITOR和BITXOR

很多人都有一个疑问:ORACLE中为什么只有BITAND而没有BITOR, BITXOR

原因是,有了BITAND, 很容易实现BITOR和BITXOR


BITOR(x,y) = (x + y) - BITAND(x, y);

BITXOR(x,y) = BITOR(x,y) - BITAND(x,y) = (x + y) - BITAND(x, y) * 2;



create or replace function to_base( p_dec in number, p_base in number ) 
return varchar2
is
l_str varchar2(255) default NULL;
l_num number default p_dec;
l_hex varchar2(16) default '0123456789ABCDEF';
begin
if ( p_dec is null or p_base is null )
then
return null;
end if;
if ( trunc(p_dec) <> p_dec OR p_dec < 0 ) then
raise PROGRAM_ERROR;
end if;
loop
l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str;
l_num := trunc( l_num/p_base );
exit when ( l_num = 0 );
end loop;
return l_str;
end to_base;
/


create or replace function to_dec
( p_str in varchar2,
p_from_base in number default 16 ) return number
is
l_num number default 0;
l_hex varchar2(16) default '0123456789ABCDEF';
begin
if ( p_str is null or p_from_base is null )
then
return null;
end if;
for i in 1 .. length(p_str) loop
l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,1)))-1;
end loop;
return l_num;
end to_dec;
/
show errors

create or replace function to_hex( p_dec in number ) return varchar2
is
begin
return to_base( p_dec, 16 );
end to_hex;
/
create or replace function to_bin( p_dec in number ) return varchar2
is
begin
return to_base( p_dec, 2 );
end to_bin;
/
create or replace function to_oct( p_dec in number ) return varchar2
is
begin
return to_base( p_dec, 8 );
end to_oct;

/
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/59111/viewspace-794505/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    13
  • 访问量
    69708