Reputation: 11
I am new to SQL. Just wanted to know the logic behind the below SQL statement:
SELECT (1 & 3)
What happens if we give &
or |
between 2 numbers in SQL statement? What is the actual logic?
I've tried with some numbers but haven't been able to understand the logic behind it.
Upvotes: 1
Views: 58
Reputation: 12971
It is a bitwise AND operation. The result of an AND operation includes the bits which are 1 in both arguments. In your example of "1 & 3", 1 in binary is 00000001 and 3 in binary is 00000011. Only the final bit is 1 in both values, so the result is 00000001. Therefore 1 & 3 = 1.
| is a bitwise OR operation, which matches bits where EITHER value is 1. Therefore 1 | 3 = 3.
A more detailed explanation can be found here.
Upvotes: 2