You are mixing up datatypes and doing lots of IMPLICIT conversions. Lets look at the utl_raw.cast_to_raw(8) first.
that is really utl_raw.cast_to_raw('8') (the number is converted into a string -- cast to raw only does STRINGS). So, we now have RAW data '8'. When you select that in sqlplus, sqlplus converts the RAW into a VARCHAR -- it doesn't know how to display RAW data -- only strings. The rules for converting from RAW to VARCHAR says to take EACH byte of the RAW data and convert it into 2 hex characters. Hence the '8' which is ascii (decimal) 56 becomes hex 83. Here is the logical progression:
number 8 - implicitly becomes ->
string '8' - explicitly becomes ->
raw '8' - implicitly become -> varchar2 again
ops$tkyte@DEV816> select utl_raw.cast_to_raw(8) from dual;
UTL_RAW.CAST_TO_RAW(8)
---------------------------
38
ops$tkyte@DEV816> select to_number(38,'xx') from dual;
TO_NUMBER(38,'XX')
------------------
56
ops$tkyte@DEV816> select chr(56) from dual;
C
-
8
that shows it did exactly what you asked.
Now, the same thing happened for 88 and 20. They were turned into '88' and '20'. You converted them into raw -- and the STRINGS '88' and '20' were bitanded, not the numbers. It bitanded for example '8' and '0'. Now, we know that
'8' = 56 decimal = 111000 binary
'0' = 48 decimal = 110000 binary
-------
110000 binary = 48 decimal = 30 hex
When I do 88 and 20, i get 3030 as the answer (not 3a3b, don't know how you got that with that data)
select utl_raw.bit_and(utl_raw.cast_to_raw(88),utl_raw.cast_to_raw(20)) from dual
ops$tkyte@DEV816> /
UTL_RAW.BIT_AND(UTL_RAW.CAST_TO_RAW(88),UTL_RAW.CAST_TO_RAW(20))
----------------------------------------------------------------
3030
which matches my expectation totally.
To get binary, you can get my hexdec packages from
</code>
http://asktom.oracle.com/~tkyte/hexdec/index.html <code>
Using that you can:
ops$tkyte@DEV816> set linesize 20
ops$tkyte@DEV816> l
1 select lpad(to_bin(88),20,'0'),
2 lpad(to_bin(20),20,'0'),
3 lpad(to_bin(bitand(88,20)),20,'0')
4* from dual
ops$tkyte@DEV816> /
LPAD(TO_BIN(88),20,'
--------------------
LPAD(TO_BIN(20),20,'
--------------------
LPAD(TO_BIN(BITAND(8
--------------------
00000000000001011000
00000000000000010100
00000000000000010000
which seems to be more like what you want.