Oracle: UTL_RAW.BIT_OR(2,1) gives me 2 instead of 3. Why

oracleoracle-11g-r2

Why am I getting 2 instead of 3 when I perform BIT_OR(2,1)?

SELECT UTL_RAW.CAST_TO_NUMBER (              --> decimal: 3? nope, 2
            UTL_RAW.BIT_OR(                  --> binary: 11? shouldn't it?
                UTL_RAW.CAST_FROM_NUMBER(2), --> binary: 10
                UTL_RAW.CAST_FROM_NUMBER(1)  --> binary:  1
            )
        )
FROM DUAL;

Best Answer

CAST_FROM_NUMBER returns RAW; if you check result of UTL_RAW.CAST_FROM_NUMBER(2) , it's not 2 but "C103" (which I believe means (03-1)* pow(10,(0xC1-0xC1))) . You need something like

SELECT   UTL_RAW.CAST_TO_BINARY_INTEGER(UTL_RAW.BIT_OR(
            UTL_RAW.CAST_FROM_BINARY_INTEGER(2), 
            UTL_RAW.CAST_FROM_BINARY_INTEGER(1)  
        ))

FROM DUAL;

Some links about internal number representations: http://www.jlcomp.demon.co.uk/number_format.html , http://docs.oracle.com/cd/E11882_01/appdev.112/e10646/oci03typ.htm
Your code returns result of C103(internal representation of 2) & C102(internal representation of 1) which is 2