How to use Bitwise Operators in MySQL

Notes:

Bitwise Operators in MySQL:

Bitwise Operators: are used to perform operation on bits.
Bitwise operators convert the given decimal number(s) to their binary equivalent number, and then they perform the operation on those bits and return the result in the form of decimal

&: Bitwise And operator:
If both LHS and RHS bits are 1 then the result will be 1, in all other cases result will be 0

|: Bitwise Or operator:
If both LHS and RHS bits are 0 then the result will be 0, in all other cases result will be 1

^: Bitwise Exclusive Or operator:
If both LHS and RHS bits are same bits then the result will be 0, otherwise the result will be 1

<<: Bitwise Left Shift operator:
Shifts bits of first number to the left by number of positions indicated by second number
firstNumber * pow(2,secondNumber)

>>: Bitwise Right Shift operator:
Shifts bits of first number to the right by number of positions indicated by second number
firstNumber / pow(2,secondNumber)

Example Code:
select 1 & 1;//1
select 2 & 5;// 0

select 1 | 1;//1
select 2 | 5;// 7

select 1 ^ 1;//0
select 2 ^ 5;// 7

select 1<<4;// 16
select 12<<2;// 48

select 16>>4;// 1
select 48>>2;// 12

Interview Questions: