SQL Bitwise Operator
Bitwise operators in SQL are used to perform operations on the individual bits of integer values. These are typically used in low-level programming tasks, such as flag checking or optimizing storage using binary logic.
What Are Bitwise Operators?
Bitwise operators work at the binary level. They compare corresponding bits of two operands and return a result based on the operator logic.
Syntax:
Student Table
Example: Bitwise AND ( & )
Output:
Example: Bitwise OR ( | )
Output:
Bitwise operators work at the binary level. They compare corresponding bits of two operands and return a result based on the operator logic.
List of SQL Bitwise Operators
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Sets each bit to 1 if both bits are 1 |
| | Bitwise OR | Sets 1 if either of the bits is 1 |
^ | Bitwise XOR | Sets each bit to 1 if bits are different |
<< | Left Shift | Shifts bits to the left |
>> | Right Shift | Shifts bits to the right |
Syntax:
SELECT (column1 & value1), (column2 | value2), ...
FROM table_name;
Student Table
ID | Name | Physics | Chemistry |
---|---|---|---|
1 | Aman | 86 | 92 |
2 | Sushant | 91 | 91 |
3 | Saumya | 98 | 98 |
Example: Bitwise AND ( & )
SELECT Physics & Chemistry AS Bitwise_AND
FROM student;
Output:
Bitwise_AND |
---|
80 |
91 |
98 |
Explanation: Performs a bitwise AND between each student's Physics and Chemistry marks.
Example: Bitwise OR ( | )
SELECT Physics | Chemistry AS Bitwise_OR
FROM student;
Output:
Bitwise_OR |
---|
98 |
91 |
98 |
Key Point
- Bitwise operations are useful in binary logic, flags, permissions, and optimization.
- Only integer values are supported.
- Mostly used in low-level logic implementations within SQL.