Reputation: 1468
I am confused about the oracle bitand function. I know it is used to cıntrol whether two bits are set. But, what is the meaning of being set. When and why it is used. If you can gve an example based on a real example, it will be so pleasent for me. Thank for your answers.
Upvotes: 2
Views: 13016
Reputation: 50017
In binary, "set" means "has the value 1". "Not set" means "has the value 0".
From the Oracle docs for BITAND:
"The result is computed in several steps. First, each argument A is replaced with the value SIGN(A)*FLOOR(ABS(A)). This conversion has the effect of truncating each argument towards zero. Next, each argument A (which must now be an integer value) is converted to an n-bit two's complement binary integer value. The two bit values are combined using a bitwise AND operation. Finally, the resulting n-bit two's complement value is converted back to NUMBER."
Put simply, this function truncates its arguments, converts them to a binary number (currently limited to 128 bits), AND's the two binary numbers together, and returns the result of converting the binary number back to a NUMBER.
Here's the result of all possible combinations of zero and one:
SELECT BITAND(0, 0) AS "0, 0", -- i.e. 0 AND 0 = 0
BITAND(0, 1) AS "0, 1", -- i.e. 0 AND 1 = 0
BITAND(1, 0) AS "1, 0", -- i.e. 1 AND 0 = 0
BITAND(1, 1) AS "1, 1" -- i.e. 1 AND 1 = 1
FROM DUAL;
A more complex example would be ANDing together 11 and 5. In binary, 11 decimal becomes "1011". 5 decimal becomes "0101" binary. If you AND these values together, as in
1 0 1 1
0 1 0 1
-------
0 0 0 1
you get 1 binary, which is still 1 when converted back to decimal.
Share and enjoy.
Upvotes: 6