ivenxu
ivenxu

Reputation: 709

How oracle handle negative values in Bitand function

Basically, I can understand the general idea of the bitand(n1, n2) function, it will convert the parameters to binary format and do bit and operator bit-by-bit, then convert back to number before return. But I can't understand how oracle calculate the negative values. There are very rare materials about the negative on the Internet. I've read the answer https://stackoverflow.com/a/10617716/1443505, it vaguely said "each argument A is replaced with the value SIGN(A)*FLOOR(ABS(A))". Base on this explanation, I still can't explain the out of the following sql,

SQL> select bitand(6,3), bitand(-6,3), bitand(-6,-3), bitand(-6,3) from dual;

BITAND(6,3) BITAND(-6,3) BITAND(-6,-3) BITAND(-6,3)
----------- ------------ ------------- ------------
          2            2            -8            2

Any answers, comments or hints are welcome.

Upvotes: 1

Views: 686

Answers (1)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

BITAND converts its arguments to binary representation of 2's complement 128-bit integers.
So, -6 is converted to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA before performing AND.

Upvotes: 1

Related Questions