BITAND function used to find a bit is set or not. It returns INTEGER.
Syntax :- BITAND(expr1,expr1)
If the values are the same, the operator returns 1 and return 0 otherwise.
BITAND function first convert both expression into binary format, then compare bit by bit and returns in decimal format
Examples:-
1. BITAND(1,1) — returns 1
2. BITAND(1,0) — returns 0
3. BITAND(0,1) — returns 0
4. BITAND(-1,-1)– returns -1
5. BITAND(null,null) – returns NULL
6. BITAND(24,18) – returns 8
Explanation:-
Binary representation of 24 is 11000
Binary representation of 15 is 1111
24 | 15 | BITAND | Result | Explanation |
1 | ||||
1 | 1 | ====> | 1 | (1 AND 1) is 1 |
0 | 1 | ====> | 0 | (0 AND 1) is 0 |
0 | 1 | ====> | 0 | (0 AND 1) is 0 |
0 | 1 | ====> | 0 | (0 AND 1) is 0 |
(Note that that bits are considered from right, least significant first)
Result is 1000 in binary format. If you convert 1000 binary into decimal format we get 8.
So BITAND(24,18) = 8
7. BITAND(6,2) – returns 2
Binary representation of 6 is 110
Binary representation of 2 is 10
BITAND returns 10 in Binary, which is 2
8. BITAND(6,3) – returns 2
Binary representation of 6 is 110
Binary representation of 2 is 11
BITAND returns 10 in Binary, which is 2
I think there is a mistake in the above calculation. For example: BITAND(24,18) = 16 (not 8)