Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: bitwise NOT operation in oracle

Re: bitwise NOT operation in oracle

From: Debu Das <debu_at_rheal.com>
Date: 22 Mar 2004 05:10:33 -0800
Message-ID: <298f0e41.0403220510.414e9bc@posting.google.com>


Tweetie Pooh <bjr601553_at_cia.gov> wrote in message news:<d119d2502625ebed40557a36f5df606d_at_news.teranews.com>...
> debu_at_rheal.com (Debu Das) honoured comp.databases.oracle.server on Thu 18 Mar
> 2004 10:31:19a with news:298f0e41.0403180231.29e9bf79_at_posting.google.com:
>
> > Hi Friend,
> >
> > Does oracle support a bitwise NOT operation?
> >
> >
> > Thanks in advance,
> >
> > Debu
>
> If you are intending to reverse the bits then surely use a bitwise XOR with a
> pattern of all bits on.
>
> 10101010
> 11111111 xor
> ========
> 01010101

Hi Friends,

I got a package created from the link http://www.jlcomp.demon.co.uk/faq/bitwise.html

It will be of great help for the others who want to use it. There is some modification made to get the bitwise NOT operation.

create or replace package bitops2 is

function bitand(p_dec1 number, p_dec2 number) return varchar2 ; function bitor(p_dec1 number, p_dec2 number) return varchar2 ; function bitxor(p_dec1 number, p_dec2 number) return varchar2 ; function raw_ascii(p_dec number) return raw; function ascii_raw(p_raw varchar2) return number; function bitnot(p_dec1 number) return number;

end;

create or replace package body bitops2 is

function raw_ascii(p_dec number) return raw is   v_result varchar2(1999);
  v_tmp1 number := p_dec;
begin
  loop
    v_result := chr(mod(v_tmp1,256)) || v_result ;     v_tmp1 := trunc(v_tmp1/256);
    exit when v_tmp1 = 0;
  end loop;
  return utl_raw.cast_to_raw(v_result);
end;

function ascii_raw(p_raw varchar2) return number is   v_result number := 0;
begin
  for i in 1 .. length(p_raw) loop
    v_result := v_result * 256 + ascii(substr(p_raw,i,1));   end loop;
  return v_result;
end;

function bitand(p_dec1 number, p_dec2 number) return varchar2 is begin
  return
   ascii_raw(

     utl_raw.cast_to_varchar2(
       utl_raw.bit_and(
         raw_ascii(p_dec1),
         raw_ascii(p_dec2)
       )
     )

   );
end;

function bitor(p_dec1 number, p_dec2 number) return varchar2 is begin
  return
   ascii_raw(

     utl_raw.cast_to_varchar2(
       utl_raw.bit_or(
         raw_ascii(p_dec1),
         raw_ascii(p_dec2)
       )
     )

   );
end;

function bitxor(p_dec1 number, p_dec2 number) return varchar2 is begin
  return
   ascii_raw(

     utl_raw.cast_to_varchar2(
       utl_raw.bit_xor(
         raw_ascii(p_dec1),
         raw_ascii(p_dec2)
       )
     )

   );
end;

function bitnot(p_dec1 number) return number is begin
  return (0 - p_dec1) - 1;
end;
end; Received on Mon Mar 22 2004 - 07:10:33 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US