Curso de MariaDB Desde la Consola/Operadores Bitwise

De WikiCabal
Ir a la navegación Ir a la búsqueda

Operadores Bitwise

Bitwise AND

 1 Val1 & Val2
 2 
 3 Convierte los valores bit a bit a binario y compara bits. 
 4 Sólo si ambos los bits correspondientes son: 1 es el bit 
 5 resultante también 1.
 6 
 7 MariaDB [(none)]> SELECT 2&1;
 8 +-----+
 9 | 2&1 |
10 +-----+
11 |   0 |
12 +-----+
13 1 row in set (0.00 sec)
14 
15 MariaDB [(none)]> SELECT 3&1;
16 +-----+
17 | 3&1 |
18 +-----+
19 |   1 |
20 +-----+
21 1 row in set (0.00 sec)
22 
23 MariaDB [(none)]> SELECT 29 & 15;
24 +---------+
25 | 29 & 15 |
26 +---------+
27 |      13 |
28 +---------+
29 1 row in set (0.00 sec)

Shift izquierda

 1 Val 1 << Val 2
 2 
 3 Convierte a un número (BIGINT) Val1 a binario y desplaza 
 4 por val2 posiciones a la izquierda.
 5 
 6 MariaDB [(none)]> SELECT 1 << 2;
 7 +--------+
 8 | 1 << 2 |
 9 +--------+
10 |      4 |
11 +--------+
12 1 row in set (0.00 sec)
13 
14 MariaDB [(none)]> SELECT 2 << 2;
15 +--------+
16 | 2 << 2 |
17 +--------+
18 |      8 |
19 +--------+
20 1 row in set (0.00 sec)

Shift derecha

 1 Val 1 >> Val 2
 2 
 3 Convierte a un número (BIGINT) Val1 a binario y desplaza 
 4 por val2 posiciones a la derecha.
 5 
 6 MariaDB [(none)]> SELECT 4 >> 1;
 7 +--------+
 8 | 4 >> 1 |
 9 +--------+
10 |      2 |
11 +--------+
12 1 row in set (0.00 sec)
13 
14 MariaDB [(none)]> SELECT 5 >> 2;
15 +--------+
16 | 5 >> 2 |
17 +--------+
18 |      1 |
19 +--------+
20 1 row in set (0.00 sec)

BIT_COUNT

 1 BIT_COUNT(n)
 2 
 3 Devuelve el número de bits que se encuentran en el argumento n.
 4 
 5 MariaDB [(none)]> SELECT BIT_COUNT(1), BIT_COUNT(2), BIT_COUNT(3), BIT_COUNT(4); 
 6 +--------------+--------------+--------------+--------------+
 7 | BIT_COUNT(1) | BIT_COUNT(2) | BIT_COUNT(3) | BIT_COUNT(4) |
 8 +--------------+--------------+--------------+--------------+
 9 |            1 |            1 |            2 |            1 |
10 +--------------+--------------+--------------+--------------+
11 1 row in set (0.00 sec)
12 
13 MariaDB [(none)]> SELECT BIT_COUNT(b'1'), BIT_COUNT(b'10'), BIT_COUNT(b'11'), BIT_COUNT(b'100');
14 +-----------------+------------------+------------------+-------------------+
15 | BIT_COUNT(b'1') | BIT_COUNT(b'10') | BIT_COUNT(b'11') | BIT_COUNT(b'100') |
16 +-----------------+------------------+------------------+-------------------+
17 |               1 |                1 |                2 |                 1 |
18 +-----------------+------------------+------------------+-------------------+
19 1 row in set (0.00 sec)

Bitwise XOR

 1 Val1 ^ Val2
 2 
 3 Convierte los valores a binario y compara bits. 
 4 Si uno y sólo uno de los bits correspondientes son 1, el bit 
 5 resultante es también 1. De lo contrario el bit resultante es 0.
 6 
 7 MariaDB [(none)]> SELECT 1 ^ 1;
 8 +-------+
 9 | 1 ^ 1 |
10 +-------+
11 |     0 |
12 +-------+
13 1 row in set (0.00 sec)
14 
15 MariaDB [(none)]> SELECT 1 ^ 0;
16 +-------+
17 | 1 ^ 0 |
18 +-------+
19 |     1 |
20 +-------+
21 1 row in set (0.00 sec)
22 
23 MariaDB [(none)]> SELECT 11 ^ 3;
24 +--------+
25 | 11 ^ 3 |
26 +--------+
27 |      8 |
28 +--------+
29 1 row in set (0.00 sec)

Bitwise OR

 1 Val 1 | Val 2
 2 
 3 Convierte los valores a binario y compara bits. Si cualquiera de 
 4 los bits correspondientes tiene un valor de 1, el bit resultante 
 5 es también 1. De lo contrario el bit resultante es 0.
 6 
 7 MariaDB [(none)]> SELECT 2|1;
 8 +-----+
 9 | 2|1 |
10 +-----+
11 |   3 |
12 +-----+
13 1 row in set (0.00 sec)
14 
15 MariaDB [(none)]> SELECT 29 | 15;
16 +---------+
17 | 29 | 15 |
18 +---------+
19 |      31 |
20 +---------+
21 1 row in set (0.00 sec)

Bitwise NOT

 1 ~n
 2 
 3 Convierte el valor de 4 bytes binario y invierte todos los bits.
 4 
 5 MariaDB [(none)]> SELECT ~1;
 6 +----------------------+
 7 | ~1                   |
 8 +----------------------+
 9 | 18446744073709551614 |
10 +----------------------+
11 1 row in set (0.00 sec)
12 
13 MariaDB [(none)]> SELECT ~(b'00000001');
14 +----------------------+
15 | ~(b'00000001')       |
16 +----------------------+
17 | 18446744073709551614 |
18 +----------------------+
19 1 row in set (0.00 sec)
20 
21 MariaDB [(none)]> SELECT 3 & ~1;
22 +--------+
23 | 3 & ~1 |
24 +--------+
25 |      2 |
26 +--------+
27 1 row in set (0.00 sec)
28 
29 MariaDB [(none)]> SELECT b'11' & ~b'00000001';
30 +----------------------+
31 | b'11' & ~b'00000001' |
32 +----------------------+
33 |                    2 |
34 +----------------------+
35 1 row in set (0.00 sec)
36 
37 MariaDB [(none)]> SELECT b'11' & b'11111110';
38 +---------------------+
39 | b'11' & b'11111110' |
40 +---------------------+
41 |                   2 |
42 +---------------------+
43 1 row in set (0.00 sec)