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 operator in PL/SQL

Re: Bitwise operator in PL/SQL

From: Gary SMITH <Gary.Smith_at_cern.ch>
Date: 1997/10/30
Message-ID: <34588359.DFCEA336@cern.ch>#1/1

Haresh,

I had a similar requirement. I needed to examine the value of a certain bit in a
number. I do not think there is an operator to do this so I wrote my own as follows:

CREATE OR REPLACE FUNCTION bit_value(num IN INTEGER,

         bit IN INTEGER)
         RETURN INTEGER
AS
     bval INTEGER;
BEGIN
     bval := num;
     FOR i IN 1 .. (bit-1)
     LOOP
          bval := TRUNC((bval/2),0);
     END LOOP;

     RETURN MOD(bval,2);

END; It works like this. If you want to find out the bit value of say bit 3 in a number
then you can write code like this:

    bitval INTEGER;

    bitval := bit_value(num, 3);

I hope this helps;

Haresh Assumal wrote:

> Hi,
> Does anyone know if there is a bitwise operator in PL/SQL. I need to
> compare bits in a number field, Eg if i=3 (101 binary) and j=1 then i AND j
> = 1, if k=2 (010 binary) then i AND k = 0.
> What I am looking for is the bitwise AND operator.
>
> Thanks,
> Haresh

--
*************************
Gary Smith
CERN, Geneva, Switzerland
Email: Gary.Smith_at_cern.ch
Tel:   +41 22 7678944
*************************
Received on Thu Oct 30 1997 - 00:00:00 CST

Original text of this message

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