Mastering MySQL 4 [Electronic resources]

Ian Gilfillan

نسخه متنی -صفحه : 229/ 160
نمايش فراداده

Bit Operators

The bit operators are not used often. They allow you to work with bit values and perform bit calculations in your queries.

&

value1 & value2

Performs a bitwise AND. This converts the values to binary and compares the bits. Only if both of the corresponding bits are 1 is the resulting bit also 1.

For example:

mysql> SELECT 2&1;
+-----+
| 2&1 |
+-----+
|   0 |
+-----+
mysql> SELECT 3&1;
+-----+
| 3&1 |
+-----+
|   1 |
+-----+

|

value1 | value2

Performs a bitwise OR. This converts the values to binary and compares the bits. If either of the corresponding bits are 1, the resulting bit is also 1.

For example:

mysql> SELECT 2|1;
+-----+
| 2|1 |
+-----+
|   3 |
+-----+

<<

value1 << value2

Converts value1 to binary and shifts the bits of value1 left by the amount of value2.

For example:

mysql> SELECT 2<<1;
+------+
| 2<<1 |
+------+
|    4 |
+------+

>>

value1 >> value2

Converts value1 to binary and shifts the bits of value1 right by the amount of value2.

For example:

mysql> SELECT 2>>1;
+------+
| 2>>1 |
+------+
|    1 |
+------+