Loading
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.



List of SQL Bitwise Operators

OperatorNameDescription
&Bitwise ANDSets each bit to 1 if both bits are 1
|Bitwise ORSets 1 if either of the bits is 1
^Bitwise XORSets each bit to 1 if bits are different
<<Left ShiftShifts bits to the left
>>Right ShiftShifts bits to the right



Syntax:

SELECT (column1 & value1), (column2 | value2), ... FROM table_name;



Student Table

IDNamePhysicsChemistry
1Aman8692
2Sushant9191
3Saumya9898



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.