Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ben Youssef.

Asked: January 17, 2001 - 11:28 am UTC

Last updated: September 29, 2017 - 1:10 pm UTC

Version: 8.0.5

Viewed 50K+ times! This question is

You Asked

Hi

i have a simple question can we use the binary operator like AND or XOR in a SQL statement. For example
"select 1 AND 1 from dual;" result = 1 or true
or
"select 1 XOR 1 from dual;"

if not, please can you tell me how can i do to have the same result as the operator AND, XOR.

thanks in advance

and Tom said...

See
</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:375818206678 <code>for more info on the bitand function.

ops$tkyte@ORA8I.WORLD> select bitand(1,1)+0 from dual;

BITAND(1,1)+0
-------------
1

ops$tkyte@ORA8I.WORLD> c/,1/,2
1* select bitand(1,2)+0 from dual
ops$tkyte@ORA8I.WORLD> /

BITAND(1,2)+0
-------------
0


and given that bitor becomes:

(x + y) - BitAND(x, y);

and bitXor becomes:

(x + y) - BitAND(x, y) * 2;

(there are other ways to implement them as well I am sure)


Rating

  (29 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, March 11, 2002 - 1:42 pm UTC


Still confused

Paul, April 02, 2003 - 3:59 am UTC

I'm trying to get my head round this bitwise stuff.

I have this system (I inherited it, I didn't design it) where a user's privileges are defined in a by their membership of a group. The privileges data was stored in an integer column in the groups table with the binary bits having the following meaning:

Bit 1 - Query access
Bit 2 - Event creation
Bit 3 - Static data admin
Bit 4 - MI Reporting
Bit 5 - User administration

ANd the groups have the following privileges set
CLERK - 00001 = 1
SUPERVISOR - 00111 = 7
MANAGER - 01001 = 9
ADMINISTRATOR - 10100 = 20

Now this was just fine and dandy when it was guaranteed that a user would only have one group. The routine would return the integer and the client (PHP) program would work out the binary stuff.

The trouble is that now users can belong to more than one group. So, for example a user might be an administrator and a supervisor. So, in this case I want to do a binary or (I think, I'm not a mathematician) and return the following:

ADMINISTRATOR - 10100 = 20
SUPERVISOR - 00111 = 7

Binary Or - 10111 = 23

So I'm trying to get a result of 23 from values of 20 and 7, and it's "doing my head in" as we say in England.

Can you help at all, please?

Thanks

Paul


Tom Kyte
April 02, 2003 - 8:07 am UTC

big_table@ORA920> select (x + y) - BitAND(x, y) bit_or
2 from ( select 20 x, 7 y from dual );

BIT_OR
----------
23

1 row selected.


x+y-bitand(x,y) does a bitor


BitOR(x, y) - BitAND(x, y);

does a bitxor



That's solved the logic but ...

Paul, April 03, 2003 - 3:31 am UTC

My problem is that I need to do a binary OR on values in a column. Now, if I know that there are exactly 2 values, I can get what I want as follows:

SQL> create table t (x number);

Table created.

SQL> insert into t values (20);

1 row created.

SQL> insert into t values (7);

1 row created.

SQL> select sum(x) - BitAnd(max(x),min(x)) from t;

SUM(X)-BITAND(MAX(X),MIN(X))
----------------------------
                          23

But what do I do if I've got more than (or less than) 2 values?

TIA

Paul 

Tom Kyte
April 03, 2003 - 7:57 am UTC

what database version are you using.

Version

Paul, April 03, 2003 - 8:00 am UTC

I'm on 8.1.7

(8.1.7.4, to be precise, my favourite ever version [so far])

Paul

Tom Kyte
April 03, 2003 - 8:20 am UTC

bummer, in 9i we could do a user defined aggregate to to a bitor.  

In 8i, we have to "fake it"


ops$tkyte@ORA817DEV> create or replace function set_bitor( p_key in number ) return number
  2  as
  3          l_bitor number := 0;
  4  begin
  5          for x in ( select sal from emp where deptno = p_key )
  6          loop
  7                  l_bitor := (l_bitor + x.sal) - bitand( l_bitor, x.sal );
  8          end loop;
  9          return l_bitor;
 10  end;
 11  /

Function created.

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> select set_bitor( 10 ) from dual;

SET_BITOR(10)
-------------
         8094

ops$tkyte@ORA817DEV> select lpad( to_bin( sal), 32, '0' ) from emp where deptno = 10;

LPAD(TO_BIN(SAL),32,'0')
--------------------------------
00000000000000000000100110010010
00000000000000000001001110001000
00000000000000000000010100010100

ops$tkyte@ORA817DEV> select to_dec( '1111110011110', 2 ) from dual;

TO_DEC('1111110011110',2)
-------------------------
                     8094

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> select deptno, set_bitor( deptno )
  2    from ( select distinct deptno from emp )
  3  /

    DEPTNO SET_BITOR(DEPTNO)
---------- -----------------
        10              8094
        20              4095
        30              4094

ops$tkyte@ORA817DEV>


 

Brilliant!!!

Paul, April 03, 2003 - 8:27 am UTC

I'll leave my question on user defined aggregates till we go to 9i! :-)

Many thanks

Paul

And now in 9i?

Tony, October 20, 2004 - 4:29 am UTC

You mentioned user defined aggregates in 9i for bitor? :-)

Thanks for all the help!!!!

/Tony



Tom Kyte
October 20, 2004 - 7:21 am UTC

search for stragg and you'll see an example implementation of a user defined aggregate - you can go from there.

Alright!!!!

A reader, October 29, 2004 - 3:49 am UTC

Two thumbs up!!!

/Tony - stragging

reg - Paul from UK comment ...

SC, December 08, 2004 - 1:23 am UTC

"Still Confused" April 02, 2004

Hi Tom,
Is the "binary bits" concept approach appropriate one for what they are using it?
Are there any alternatives?
Thanks

Tom Kyte
December 08, 2004 - 10:23 am UTC

? not sure what you are trying to say.

SC

A reader, December 09, 2004 - 5:13 am UTC

Hi Tom.
I meant the way they approached "privileges data" by using bit map.
I asked this as I was thinking of using bit map approach to select data once only from a certain table for lets say different categories.
So we have categories A, B and C and they are coded in a column as A=1, B=2, C=4. The column value is set to 0. Now if I pick up the data for category A and C- lets say 10 rows, I change the value of that column to 5 indicating that these 10 rows will not be processed anymore by A and C. Is this appropriate approach?
Thanks.

Tom Kyte
December 09, 2004 - 1:16 pm UTC

sorry, just not following you at all here.

Xor on Strings

Rajesh, July 06, 2005 - 4:49 am UTC

This was helpful for numbers. Now I have to do 'xor' operation on ebcdic string. This 80 character length ebcdic string is divided into 10 blocks each of 8 bytes. Then 1st block is xor with 2nd block. the result is then xor with the 3rd one. Like this upto the 10th block. The result string should be in Hexa. Is there any function to perform xor operation on strings. I used utl_raw.bit_xor. Please help me. It is urgent.

Tom Kyte
July 06, 2005 - 7:56 am UTC

utl_raw.cast_to_raw the "string" and you can bit_xor it.

Xor with strings

Rajesh, July 07, 2005 - 12:30 am UTC

Hi Tom,

declare
as_str varchar2(80):='ABC1234567890 SE05672461DBB0219 0061226001 BLJ000120020090 ';
ebc_str varchar2(80);

type ebc_tab is table of varchar2(8)
index by BINARY_INTEGER;

type ebc_tab_sec is table of varchar2(16)
index by BINARY_INTEGER;

i number;
r1 raw(16);

e1_tab ebc_tab;
e1_tab_sec ebc_tab_sec;

begin
ebc_str:=convert(as_str,'WE8EBCDIC500','US7ASCII');
for i in 1..10 loop
e1_tab(i):=substr(ebc_str,((i-1)*8)+1,8);
dbms_output.put_line('EBCDIC '|| i ||' is '||e1_tab(i));
end loop;
i:=1;
r1:=utl_raw.bit_xor(utl_raw.cast_to_raw(e1_tab(i)),utl_raw.cast_to_raw(e1_tab(i+1)));
dbms_output.put_line('Converted Ebcdic after xor ' ||(i+1)|| ' is '||r1);
dbms_output.put_line(utl_raw.cast_to_varchar2(r1));
end;

Result is
EBCDIC 1 is ÁÂÃñòóôõ
EBCDIC 2 is ö÷øùð@@@
EBCDIC 3 is @@@@âÅðð
EBCDIC 4 is ðððôöñÄÂ
EBCDIC 5 is Âðòñù@@@
EBCDIC 6 is ððöñòòöð
EBCDIC 7 is ðñ@@@@@@
EBCDIC 8 is @@@@ÔèÙð
EBCDIC 9 is ððððððòð
EBCDIC 10 is ðùð@@@@@
Converted Ebcdic after xor 2 is 37353B0802B3B4B5
75;..³´µ

Now I have to 'xor' the result with the 3rd block. the result is of 16 bytes while the 3rd block is of 8 bytes. How will I perform the 'xor'. Now I think I am moving in the wrong way.

Tom Kyte
July 07, 2005 - 9:10 am UTC

I don't know which way you are moving? Not sure what the issue is, it is not very clear.

so, you have 16 bytes here and 8 bytes there -- what do you want to do with them??

Xor on strings

Rajesh, July 08, 2005 - 6:33 am UTC

I got it. After converting the ascii string to ebcdic string of 10 blocks(each block 8 bytes), I have to convert each block into raw data type. Then performed the xor operation. After performing 'xor' for all the blocks(each one of raw data type), I have to convert the final result to varchar2. Then I used the rawtohex function to get the desired result.

Please visit this site. This gave me the clue. </code> http://www.jlcomp.demon.co.uk/faq/bitwise.html <code>



ASCII TO EBCDIC

Rajesh, July 29, 2005 - 7:18 am UTC

ebc_str:=convert(as_str,'WE8EBCDIC500','US7ASCII') returns
EBCDIC ÁÂÃñòóôõö÷øùð@@@@@@@âÅðððððôöñÄÂÂðòñù@@@ððöñòòöððñ@@@@@@@@@@ÔèÙðððððððòððùð@@@@@
Version:Connected to Oracle9i Enterprise Edition Release 9.2.0.1.0
-----------------------------------------------------------
The same function is returning
¿¿¿¿¿¿¿¿¿¿¿¿@@@@@@@¿¿¿¿¿¿¿¿¿°¿¿¿@@@¿¿¿¿¿¿¿¿¿¿@@@@@@@@@@¿¿¿¿¿¿¿¿¿¿¿¿¿@@@@@ some other value in another database. Why?
Version:Connected to Oracle9i Enterprise Edition Release 9.2.0.5.0

Tom Kyte
July 29, 2005 - 9:17 am UTC

are your character sets even remotely the same.

bitwise shift

Julius, July 29, 2005 - 1:41 pm UTC

Interesting thread ... Any idea if it's possible to do a bitwise shift in pl/sql? I know I can do it in java, but maybe oracle has somethig similar to bitand ...

Tom Kyte
July 29, 2005 - 2:59 pm UTC

I just divide :)  or multiply.



to_bin is here
http://asktom.oracle.com/~tkyte/hexdec/index.html

ops$tkyte@ORA9IR2> column bin1 format a15
ops$tkyte@ORA9IR2> column bin2 format a15
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> select lpad( to_bin( 3123 ), 15, '0' ) bin1,
  2         lpad( to_bin( trunc(3123/power(2,(rownum-1))) ), 15, '0' )  bin2,
  3             rownum-1 shift
  4    from all_users
  5   where rownum <= 10;
 
BIN1            BIN2                 SHIFT
--------------- --------------- ----------
000110000110011 000110000110011          0
000110000110011 000011000011001          1
000110000110011 000001100001100          2
000110000110011 000000110000110          3
000110000110011 000000011000011          4
000110000110011 000000001100001          5
000110000110011 000000000110000          6
000110000110011 000000000011000          7
000110000110011 000000000001100          8
000110000110011 000000000000110          9
 
10 rows selected.
 
 

AScii to EBCDIC

Rajesh, August 01, 2005 - 2:42 am UTC

My character Sets are different. In the first case it is US7ASCII and the second case it is UTF8. So what changes I should do, in my code so that I will get the expected result.
I am expecting a solution, where I need not have to change the NLS_CHARACTERSET.

Please help!!!! It's urgent

Tom Kyte
August 01, 2005 - 7:34 am UTC

well, is the data itself the same -- how for example -- did you get ebcdic data into a utf database, I would expect that to cause all kinds of errors (since edcdic is a single byte encoding scheme, much like us7ascii but utf is a multibyte one)

ASCII TO EBCDIC

Rajesh, August 01, 2005 - 7:40 am UTC

Is there any method to get the EBCDIC string then UTF8.



Tom Kyte
August 01, 2005 - 7:48 am UTC

I don't know, I don't know your situation and what is possible - I don't know your environment, your tools, nothing.

I'd be wondering how you got ebcdic into utf8 in the first place myself here. US7ASCII I can see (just set your NLS_LANG in your environment the us7ascii and load into a database with us7ascii and it would just load the single byte data).

ORA-1426

Arun Mathur, August 29, 2006 - 3:34 pm UTC

Tom,

I enjoyed building and using the code you supplied (ie to_base, to_bin, to_hex,etc..).  It works great! I slightly modified it such that the functions are part of the same package, and expanded the width of the l_str variable.   

Interesting occurence: I threw an ORA-1426 (numeric overflow error) when performing a to_bin function with power(2,161).  

See below:

SQL> conn amathur@dbdev
Connected.
SQL> @pkg_num_conversions.sql
SQL> create or replace package pkg_num_conversions is
  2  function to_base(p_dec in number,p_base in number) return varchar2;
  3  function to_dec(p_str in varchar2,p_from_base in number default 16) return number;
  4  function to_hex(p_dec in number) return varchar2;
  5  function to_bin(p_dec in number) return varchar2;
  6  function to_oct(p_dec in number) return varchar2;
  7  end;
  8  /

Package created.

SQL> create or replace package body pkg_num_conversions is
  2  function to_base( p_dec in number, p_base in number )
  3  return varchar2
  4  is
  5           l_str   varchar2(4096) default NULL;
  6           l_num   number  default p_dec;
  7           l_hex   varchar2(16) default '0123456789ABCDEF';
  8  begin
  9           if ( p_dec is null or p_base is null )
 10           then
 11               return null;
 12           end if;
 13           if ( trunc(p_dec) <> p_dec OR p_dec < 0 ) then
 14               raise PROGRAM_ERROR;
 15           end if;
 16           loop
 17               l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str;
 18               l_num := trunc( l_num/p_base );
 19               exit when ( l_num = 0 );
 20           end loop;
 21           return l_str;
 22  end to_base;
 23  function to_dec
 24  ( p_str in varchar2,
 25    p_from_base in number default 16 ) return number
 26  is
 27           l_num   number default 0;
 28           l_hex   varchar2(16) default '0123456789ABCDEF';
 29  begin
 30           if ( p_str is null or p_from_base is null )
 31           then
 32               return null;
 33           end if;
 34           for i in 1 .. length(p_str) loop
 35               l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,1)))-1;
 36           end loop;
 37           return l_num;
 38  end to_dec;
 39  function to_hex( p_dec in number ) return varchar2
 40  is
 41  begin
 42           return to_base( p_dec, 16 );
 43  end to_hex;
 44  function to_bin( p_dec in number ) return varchar2
 45  is
 46  begin
 47           return to_base( p_dec, 2 );
 48  end to_bin;
 49  function to_oct( p_dec in number ) return varchar2
 50  is
 51  begin
 52           return to_base( p_dec, 8 );
 53  end to_oct;
 54  end;
 55  /

Package body created.

SQL> show errors
No errors.
SQL> select pkg_num_conversions.to_bin(power(2,160)) from dual;

PKG_NUM_CONVERSIONS.TO_BIN(POWER(2,160))                                        
--------------------------------------------------------------------------------
10000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000010000000000000000000000000         
                                                                                

SQL> select pkg_num_conversions.to_bin(power(2,161)) from dual;
select pkg_num_conversions.to_bin(power(2,161)) from dual
       *
ERROR at line 1:
ORA-01426: numeric overflow 
ORA-06512: at "AMATHUR.PKG_NUM_CONVERSIONS", line 17 
ORA-06512: at "AMATHUR.PKG_NUM_CONVERSIONS", line 47 


SQL> 
SQL> select text from user_source
  2  where name='PKG_NUM_CONVERSIONS'
  3  and line=17;

TEXT                                                                            
--------------------------------------------------------------------------------
        l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str;                    

SQL> 3
  3* and line=17
SQL> c/17/47
  3* and line=47
SQL> /

TEXT                                                                            
--------------------------------------------------------------------------------
    return to_base( p_dec, 2 );                                                    

SQL> select * from v$version;

BANNER                                                                          
----------------------------------------------------------------                
Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production                
PL/SQL Release 9.2.0.1.0 - Production                                           
CORE    9.2.0.1.0    Production                                                       
TNS for Solaris: Version 9.2.0.1.0 - Production                                 
NLSRTL Version 9.2.0.1.0 - Production                                           

SQL> spool off;

So, my question is: Do you know of any Oracle links which may specify the maximum number that the mod function accepts? I checked the Oracle 9.2 docs as well as Metalink articles, but so far haven't found anything.

Thanks again, Tom. Hope all is well.

Regards,
Arun 

Tom Kyte
August 29, 2006 - 5:33 pm UTC

interesting, that results in a 49 digit number that exceeds the precision of the Oracle number type greatly.  The mod actually appears to work (but funny)

ops$tkyte%ORA10GR2> select mod( power(2,161), 2 ) from dual;

MOD(POWER(2,161),2)
-------------------
         -1.000E+10

1 row selected.


it went negative.  but even at 160, something is wrong:

ops$tkyte%ORA10GR2> select mod( power(2,160), 2 ) from dual;

MOD(POWER(2,160),2)
-------------------
                  0

1 row selected.

ops$tkyte%ORA10GR2> select mod( power(2,160)+1, 2 ) from dual;

MOD(POWER(2,160)+1,2)
---------------------
                    0

1 row selected.

(they cannot both be 0) 

No. of 1's

Deepak, January 31, 2008 - 7:41 am UTC

Hi Tom,

Have the following requirement.

Input : 6 (Binary representation - 0110)
Output : 2 (No. of 1's in the input's binary string)

Input : 10 (Binary representation - 1010)
Output : 2 (No. of 1's in the input's binary string)

Input : 14 (Binary representation - 1110)
Output : 3 (No. of 1's in the input's binary string)

What would be the most efficient way to implement the above.

Please help.
Tom Kyte
February 04, 2008 - 3:13 pm UTC

ops$tkyte%ORA10GR2> with data as (select '1110' bin from dual)
  2  select length(bin)-length(replace(bin,'1','')) from data;

LENGTH(BIN)-LENGTH(REPLACE(BIN,'1',''))
---------------------------------------
                                      3

ops$tkyte%ORA10GR2> with data as (select '1010' bin from dual)
  2   select length(bin)-length(replace(bin,'1','')) from data;

LENGTH(BIN)-LENGTH(REPLACE(BIN,'1',''))
---------------------------------------
                                      2


No. of 1's

agustinldg, February 03, 2008 - 7:17 pm UTC

Funny, I've had the same requirement, at almost the same time.

I first used Tom's base conversion routines (to_bin)
http://asktom.oracle.com/tkyte/hexdec/index.html

Example of calculating the 1's in the binary representation of 789:

SQL> Select 789 N, to_bin(789) bin,
  2    nvl(
  3      length(
  4        replace(
  5          to_bin(789)
  6        ,'0','')
  7      )
  8    ,0) NUMBER_OF_1S from dual;

         N BIN             NUMBER_OF_1S
---------- --------------- ------------
       789 1100010101                 5

But then, to avoid the overhead involved in calling plsql from sql, I've come up with another solution:

SQL> Select 789 N, to_bin(789) bin ,
  2    nvl(
  3      length(
  4        replace(
  5          replace(
  6            replace(
  7              translate(
  8                to_char(789,'FMXXXXXXXX') 
  9              ,'24839AC657DEB0','1112222223333')
 10            ,'2','11')
 11          ,'3','111')
 12        ,'F','1111')
 13      )
 14    ,0) NUMBER_OF_1S from dual;

         N BIN             NUMBER_OF_1S
---------- --------------- ------------
       789 1100010101                 5

The number to be processed is first converted to hexadecimal and then, every digit is replaced by the number of 1's in its binary representation. Finally, the 1's are counted with the length function.


I've tested the second algorithm against the first for all 2 bytes numbers:

SQL> with test
  2   as
  3   (select level-1 n
  4      from dual
  5    connect by level <= power( 2,16)
  6   )
  7  select n from test
  8  where  nvl(length(replace(replace(replace( translate(to_char(n,'FMXXXXXXXX'),
  9  '24839AC657DEB0','1112222223333'),'2','11'),'3','111'),'F','1111')),0) 
 10   <> nvl(length(replace(to_bin(n),'0','')),0) ;

no rows selected


And, with oracle9iR2, I've checked the execution times when they are used inside SQL.

with test1
 as
 (select level-1 n
  from dual
  connect by level <= 1000000
 )
select sum(nvl(length(replace(to_bin(n),'0','')),0)) n_total_1s
from test1

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2    125.67     125.75          0          3          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4    125.67     125.75          0          3          0           1

********************************************************************************

with test2
 as
 (select level-1 n
  from dual
  connect by level <= 1000000
 )
select sum( nvl(length(replace(replace(replace( translate(to_char(n,'FMXXXXXXXX')
,'24839AC657DEB0','1112222223333'),'2','11'),'3','111'),'F','1111')),0) ) n_total_1s
from test2

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      5.93       5.92          0          3          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      5.93       5.92          0          3          0           1



Hope this can help you!

(Don't forget to adjust the format in the to_char function if you are going to analyze numbers bigger than 4 bytes.)



Thanks a lot agustinldg

Deepak, February 04, 2008 - 2:30 am UTC

Thanks a lot agustinldg. But could not really understand the following...

translate(to_char(789,'FMXXXXXXXX'),'24839AC657DEB0','1112222223333')




No of 1's in a binary string

Michel Cadot, February 04, 2008 - 3:46 pm UTC


I gave I answer of this at:
http://www.orafaq.com/forum/mv/msg/96535/297439/102589/#msg_297439

Regards
Michel

No of 1's in a binary string

Michel Cadot, February 04, 2008 - 4:13 pm UTC


To be more precise the answer was:
SQL> col bin format a10
SQL> def N=6
SQL> select reverse(max(replace(sys_connect_by_path(mod(trunc(&N/power(2,level-1)),2),' '),' ',''))) bin,
  2         sum(mod(trunc(&N/power(2,level-1)),2)) Ones
  3  from dual
  4  connect by level <= 10
  5  /
BIN         ONES
---------- -----
0000000110     2

1 row selected.

SQL> def n=10
SQL> /
BIN         ONES
---------- -----
0000001010     2

1 row selected.

SQL> def n=14
SQL> /
BIN         ONES
---------- -----
0000001110     3

1 row selected.

Regards
Michel

Number to binary conversion

Girish Sharma, April 25, 2008 - 1:53 am UTC

Thanks a lot Michel for your help.
It would be most useful if you please provide us the code; by which we can get any number's binary representation, i.e. 25,2008,-25,-2008,25.23,-25.23,2008.0701,-1715.1253 etc. (any number value).

Thanks & Kind Regards
Girish Sharma

XOR operator

SQB, September 10, 2013 - 10:03 am UTC

Tom, is there any chance of an XOR operator being added?

It would be useful for defining constraints where only one of two fields should be filled.
ALTER TABLE x ADD CONSTRAINT chk_x CHECK(y IS NULL XOR z IS NULL)

Tom Kyte
September 10, 2013 - 10:24 am UTC

no xor in the language forthcoming as far as I know.

XOR in constraints...

Toon Koppelaars, September 11, 2013 - 7:51 am UTC

Your XOR requirement can of course be easily worked around by just adding two CHECK's:

alter table T add check(col1 is null or col2 is null);
alter table T add check(col1 is not null or col2 is not null);

Tom Kyte
September 23, 2013 - 5:40 pm UTC

or just

check ((col1 is null and col2 is not null) or (col1 is not null and col2 is null));

or

check(col1 is null or col2 is null) AND (col1 is not null or col2 is not null );

Conjunctive normal form

Toon Koppelaars, September 24, 2013 - 8:38 am UTC

I always prefer the conjunctive normal form for specifying constraints (that is of the form: A and B and C and ...).

And then declaring a separate CHECK clause *per* conjunct.
That way the error-messages, in case one gets violated, are as detailed as possible.


number of bits

Connor, November 26, 2013 - 9:38 am UTC

int bitcount(unsigned int n) {

register unsigned int tmp;

tmp = n - ((n >> 1) & 033333333333)
- ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}

google 'hakmem'

on in plsql if you prefer

Connor, November 26, 2013 - 9:50 am UTC

declare
n number := ....;
tmp number;
begin
tmp := n - ( bitand ((n / 2) ,3681400539))
- (bitand ((n / 4) ,1227133513));

tmp := mod ( bitand ((tmp + (tmp / 8)) , 3340530119) , 63 );
dbms_output.put_line(tmp);
end;

March Hare

A reader, September 26, 2017 - 2:08 pm UTC

I think you stink.
тупой мудак

Connor McDonald
September 29, 2017 - 1:10 pm UTC

Aw....a cute bunny comment

:-)