Oracle按位运算符

来源:互联网 发布:手机变砖刷机软件 编辑:程序博客网 时间:2024/05/18 01:49
 

利用bitand实现多种位操作

转:http://www.oracle.com.cn/viewthread.php?tid=83181
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;
实现了位操作| ,^, &, <<, >> ,取一个整数某位的值,为了简明起见,大部分没有判断参数合法性,效率和系统bitand在一个数量级
请各位检查是否有其他错误

create or replace function bitor0(x IN NUMBER,y IN NUMBER)
return number is
n_result number;
begin
n_result:=(x + y) - BITAND(x, y);
return n_result;
end;
/
create or replace function bitor(x IN NUMBER,y IN NUMBER)
return number is
begin
return (x + y) - BITAND(x, y);
end;
/
create or replace function bitxor0(x IN NUMBER,y IN NUMBER) --faster
return number is
begin
return (x + y) - BITAND(x, y)*2;
end;
/
create or replace function bitxor(x IN NUMBER,y IN NUMBER) --slower
return number is
begin
return (x + y) - BITAND(x, y)- BITAND(x, y);
end;
/
create or replace function bitxor(x IN NUMBER,y IN NUMBER) --fast
return number is
n_result number;
begin
n_result:= BITAND(x, y);
return (x + y) - n_result- n_result;
end;
/
create or replace function bitnot(x IN NUMBER) --~x=x^0xffff
return number is
n_result number;
begin
n_result:= BITAND(x, -1);
return (x -1 ) - n_result- n_result;
end;
/
create or replace function bitlmv(x IN NUMBER,y IN NUMBER)
return number is
begin
return x* power(2,y);
end;
/
create or replace function bitrmv(x IN NUMBER,y IN NUMBER)
return number is
begin
return trunc(x/ power(2,y));
end;
/
create or replace function bitget(x IN NUMBER,y IN NUMBER)
return number is
n_result number;
begin
n_result :=power(2,y-1);
return bitand(x,n_result)/n_result;
end;
/
create or replace function bitgeta(x IN NUMBER,y IN NUMBER)
return number is
n_result number;
begin
n_result :=power(2,y-1);
if bitand(x,n_result)>0 then
return 1;
else
return 0;
end if;
end;
/
原创粉丝点击