Curso de MariaDB Desde la Consola/Operadores Bitwise

De WikiCabal
Saltar a: navegación, buscar

Operadores Bitwise

Bitwise AND

Val1 & Val2

Convierte los valores bit a bit a binario y compara bits. 
Sólo si ambos los bits correspondientes son: 1 es el bit 
resultante también 1.

MariaDB [(none)]> SELECT 2&1;
+-----+
| 2&1 |
+-----+
|   0 |
+-----+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 3&1;
+-----+
| 3&1 |
+-----+
|   1 |
+-----+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 29 & 15;
+---------+
| 29 & 15 |
+---------+
|      13 |
+---------+
1 row in set (0.00 sec)

Shift izquierda

Val 1 << Val 2

Convierte a un número (BIGINT) Val1 a binario y desplaza 
por val2 posiciones a la izquierda.

MariaDB [(none)]> SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
|      4 |
+--------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 2 << 2;
+--------+
| 2 << 2 |
+--------+
|      8 |
+--------+
1 row in set (0.00 sec)

Shift derecha

Val 1 >> Val 2

Convierte a un número (BIGINT) Val1 a binario y desplaza 
por val2 posiciones a la derecha.

MariaDB [(none)]> SELECT 4 >> 1;
+--------+
| 4 >> 1 |
+--------+
|      2 |
+--------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 5 >> 2;
+--------+
| 5 >> 2 |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

BIT_COUNT

BIT_COUNT(n)

Devuelve el número de bits que se encuentran en el argumento n.

MariaDB [(none)]> SELECT BIT_COUNT(1), BIT_COUNT(2), BIT_COUNT(3), BIT_COUNT(4); 
+--------------+--------------+--------------+--------------+
| BIT_COUNT(1) | BIT_COUNT(2) | BIT_COUNT(3) | BIT_COUNT(4) |
+--------------+--------------+--------------+--------------+
|            1 |            1 |            2 |            1 |
+--------------+--------------+--------------+--------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT BIT_COUNT(b'1'), BIT_COUNT(b'10'), BIT_COUNT(b'11'), BIT_COUNT(b'100');
+-----------------+------------------+------------------+-------------------+
| BIT_COUNT(b'1') | BIT_COUNT(b'10') | BIT_COUNT(b'11') | BIT_COUNT(b'100') |
+-----------------+------------------+------------------+-------------------+
|               1 |                1 |                2 |                 1 |
+-----------------+------------------+------------------+-------------------+
1 row in set (0.00 sec)

Bitwise XOR

Val1 ^ Val2

Convierte los valores a binario y compara bits. 
Si uno y sólo uno de los bits correspondientes son 1, el bit 
resultante es también 1. De lo contrario el bit resultante es 0.

MariaDB [(none)]> SELECT 1 ^ 1;
+-------+
| 1 ^ 1 |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 1 ^ 0;
+-------+
| 1 ^ 0 |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 11 ^ 3;
+--------+
| 11 ^ 3 |
+--------+
|      8 |
+--------+
1 row in set (0.00 sec)

Bitwise OR

Val 1 | Val 2

Convierte los valores a binario y compara bits. Si cualquiera de 
los bits correspondientes tiene un valor de 1, el bit resultante 
es también 1. De lo contrario el bit resultante es 0.

MariaDB [(none)]> SELECT 2|1;
+-----+
| 2|1 |
+-----+
|   3 |
+-----+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 29 | 15;
+---------+
| 29 | 15 |
+---------+
|      31 |
+---------+
1 row in set (0.00 sec)

Bitwise NOT

~n

Convierte el valor de 4 bytes binario y invierte todos los bits.

MariaDB [(none)]> SELECT ~1;
+----------------------+
| ~1                   |
+----------------------+
| 18446744073709551614 |
+----------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT ~(b'00000001');
+----------------------+
| ~(b'00000001')       |
+----------------------+
| 18446744073709551614 |
+----------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT 3 & ~1;
+--------+
| 3 & ~1 |
+--------+
|      2 |
+--------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT b'11' & ~b'00000001';
+----------------------+
| b'11' & ~b'00000001' |
+----------------------+
|                    2 |
+----------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT b'11' & b'11111110';
+---------------------+
| b'11' & b'11111110' |
+---------------------+
|                   2 |
+---------------------+
1 row in set (0.00 sec)