Loading

MySql Bitwise Operators

Bitwise Operators basically compare with bit value. That means it performs an operation on a bit. That means before the operation first it converts the given decimal number to its binary equivalent and then after the completion of the operation, it gives the decimal result. For example, if we want to find the age of users then first we are required to convert the user age value from integer to binary.
&: Bitwise And operator: MySQL BITWISE AND operator return an unsigned 64-bit integer. If both left-hand side and right-hand side bits are 1 then the result will be 1, in all other cases result will be 0
mysql> SELECT 3 & 15; // 3
mysql> SELECT 6 & ~4;//2
Get the odd employee age from the EMPLOYEES table.
SELECT EMP_AGE FROM EMPLOYEES WHERE EMP_AGE & 1
|: Bitwise Or operator: If both side values become zero then the result will be zero and the rest of all case’s values become 1. bitwise OR operator returns an unsigned 64-bit integer.
mysql> SELECT 2 | 7; //7
mysql> SELECT 10 | 6;//14
^: Bitwise Exclusive Or operator: If both left-hand side and right-hand side bits are the same bits then the result will be 0, otherwise the result will be 1
mysql> select 1 ^ 1//0
mysql> select 2 ^ 3//1
<<: Bitwise Left Shift operator: this operator is used to move all the bits to the left. The return value will be zero when the shift count is greater than or equal to the width of an unsigned number.
mysql> SELECT 2 << 6;//128
mysql> SELECT 1 << 1;//2
>>: Bitwise Right Shift operator: this operator is used to move all the bits to the right.
The return value will be zero when the shift count is greater than or equal to the width of an unsigned number.
mysql> SELECT 2 >> 6;//0
mysql> SELECT 1 >> 1;//0

Share with:


Leave a Reply

Connect with:

z35W7z4v9z8w