Rechercher dans le manuel MySQL

12.12 Bit Functions and Operators

Table 12.16 Bit Functions and Operators

Name Description
BIT_COUNT() Return the number of bits that are set
& Bitwise AND
~ Bitwise inversion
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift

Bit functions and operators comprise BIT_COUNT(), BIT_AND(), BIT_OR(), BIT_XOR(), &, |, ^, ~, <<, and >>. (The BIT_AND(), BIT_OR(), and BIT_XOR() aggregate functions are described in Section 12.20.1, “Aggregate (GROUP BY) Function Descriptions”.) Prior to MySQL 8.0, bit functions and operators required BIGINT (64-bit integer) arguments and returned BIGINT values, so they had a maximum range of 64 bits. Non-BIGINT arguments were converted to BIGINT prior to performing the operation and truncation could occur.

In MySQL 8.0, bit functions and operators permit binary string type arguments (BINARY, VARBINARY, and the BLOB types) and return a value of like type, which enables them to take arguments and produce return values larger than 64 bits. Nonbinary string arguments are converted to BIGINT and processed as such, as before.

An implication of this change in behavior is that bit operations on binary string arguments might produce a different result in MySQL 8.0 than in 5.7. For information about how to prepare in MySQL 5.7 for potential incompatibilities between MySQL 5.7 and 8.0, see Bit Functions and Operators, in MySQL 5.7 Reference Manual.

Bit Operations Prior to MySQL 8.0

Bit operations prior to MySQL 8.0 handle only unsigned 64-bit integer argument and result values (that is, unsigned BIGINT values). Conversion of arguments of other types to BIGINT occurs as necessary. Examples:

  • This statement operates on numeric literals, treated as unsigned 64-bit integers:

    1. mysql> SELECT 127 | 128, 128 << 2, BIT_COUNT(15);
    2. +-----------+----------+---------------+
    3. | 127 | 128 | 128 << 2 | BIT_COUNT(15) |
    4. +-----------+----------+---------------+
    5. |       255 |      512 |             4 |
    6. +-----------+----------+---------------+
  • This statement performs to-number conversions on the string arguments ('127' to 127, and so forth) before performing the same operations as the first statement and producing the same results:

    1. mysql> SELECT '127' | '128', '128' << 2, BIT_COUNT('15');
    2. +---------------+------------+-----------------+
    3. | '127' | '128' | '128' << 2 | BIT_COUNT('15') |
    4. +---------------+------------+-----------------+
    5. |           255 |        512 |               4 |
    6. +---------------+------------+-----------------+
  • This statement uses hexadecimal literals for the bit-operation arguments. MySQL by default treats hexadecimal literals as binary strings, but in numeric context evaluates them as numbers (see Section 9.1.4, “Hexadecimal Literals”). Prior to MySQL 8.0, numeric context includes bit operations. Examples:

    1. mysql> SELECT X'7F' | X'80', X'80' << 2, BIT_COUNT(X'0F');
    2. +---------------+------------+------------------+
    3. | X'7F' | X'80' | X'80' << 2 | BIT_COUNT(X'0F') |
    4. +---------------+------------+------------------+
    5. |           255 |        512 |                4 |
    6. +---------------+------------+------------------+

    Handling of bit-value literals in bit operations is similar to hexadecimal literals (that is, as numbers).

Contents Haut

Bit Operations in MySQL 8.0

MySQL 8.0 extends bit operations to handle binary string arguments directly (without conversion) and produce binary string results. (Arguments that are not integers or binary strings are still converted to integers, as before.) This extension enhances bit operations in the following ways:

  • Bit operations become possible on values longer than 64 bits.

  • It is easier to perform bit operations on values that are more naturally represented as binary strings than as integers.

For example, consider UUID values and IPv6 addresses, which have human-readable text formats like this:

UUID: 6ccd780c-baba-1026-9564-5b8c656024db
IPv6: fe80::219:d1ff:fe91:1a72

It is cumbersome to operate on text strings in those formats. An alternative is convert them to fixed-length binary strings without delimiters. UUID_TO_BIN() and INET6_ATON() each produce a value of data type BINARY(16), a binary string 16 bytes (128 bits) long. The following statements illustrate this (HEX() is used to produce displayable values):

  1. mysql> SELECT HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'));
  2. +----------------------------------------------------------+
  3. | HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db')) |
  4. +----------------------------------------------------------+
  5. | 6CCD780CBABA102695645B8C656024DB                         |
  6. +----------------------------------------------------------+
  7. mysql> SELECT HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72'));
  8. +---------------------------------------------+
  9. | HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72')) |
  10. +---------------------------------------------+
  11. | FE800000000000000219D1FFFE911A72            |
  12. +---------------------------------------------+

Those binary values are easily manipulable with bit operations to perform actions such as extracting the timestamp from UUID values, or extracting the network and host parts of IPv6 addresses. (For examples, see later in this discussion.)

Arguments that count as binary strings include column values, routine parameters, local variables, and user-defined variables that have a binary string type: BINARY, VARBINARY, or one of the BLOB types.

What about hexadecimal literals and bit literals? Recall that those are binary strings by default in MySQL, but numbers in numeric context. How are they handled for bit operations in MySQL 8.0? Does MySQL continue to evaluate them in numeric context, as is done prior to MySQL 8.0? Or do bit operations evaluate them as binary strings, now that binary strings can be handled natively without conversion?

Answer: It has been common to specify arguments to bit operations using hexadecimal literals or bit literals with the intent that they represent numbers, so MySQL continues to evaluate bit operations in numeric context when all bit arguments are hexadecimal or bit literals, for backward compatility. If you require evaluation as binary strings instead, that is easily accomplished: Use the _binary introducer for at least one literal.

  • These bit operations evaluate the hexadecimal literals and bit literals as integers:

    1. mysql> SELECT X'40' | X'01', b'11110001' & b'01001111';
    2. +---------------+---------------------------+
    3. | X'40' | X'01' | b'11110001' & b'01001111' |
    4. +---------------+---------------------------+
    5. |            65 |                        65 |
    6. +---------------+---------------------------+
  • These bit operations evaluate the hexadecimal literals and bit literals as binary strings, due to the _binary introducer:

    1. mysql> SELECT _binary X'40' | X'01', b'11110001' & _binary b'01001111';
    2. +-----------------------+-----------------------------------+
    3. | _binary X'40' | X'01' | b'11110001' & _binary b'01001111' |
    4. +-----------------------+-----------------------------------+
    5. | A                     | A                                 |
    6. +-----------------------+-----------------------------------+

Although the bit operations in both statements produce a result with a numeric value of 65, the second statement operates in binary-string context, for which 65 is ASCII A.

In numeric evaluation context, permitted values of hexadecimal literal and bit literal arguments have a maximum of 64 bits, as do results. By contrast, in binary-string evaluation context, permitted arguments (and results) can exceed 64 bits:

  1. mysql> SELECT _binary X'4040404040404040' | X'0102030405060708';
  2. +---------------------------------------------------+
  3. | _binary X'4040404040404040' | X'0102030405060708' |
  4. +---------------------------------------------------+
  5. | ABCDEFGH                                          |
  6. +---------------------------------------------------+

There are several ways to refer to a hexadecimal literal or bit literal in a bit operation to cause binary-string evaluation:

  1. _binary literal
  2. BINARY literal
  3. CAST(literal AS BINARY)

Another way to produce binary-string evaluation of hexadecimal literals or bit literals is to assign them to user-defined variables, which results in variables that have a binary string type:

  1. mysql> SET @v1 = X'40', @v2 = X'01', @v3 = b'11110001', @v4 = b'01001111';
  2. mysql> SELECT @v1 | @v2, @v3 & @v4;
  3. +-----------+-----------+
  4. | @v1 | @v2 | @v3 & @v4 |
  5. +-----------+-----------+
  6. | A         | A         |
  7. +-----------+-----------+

In binary-string context, bitwise operation arguments must have the same length or an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs:

  1. mysql> SELECT _binary X'40' | X'0001';
  2. ERROR 3513 (HY000): Binary operands of bitwise
  3. operators must be of equal length

To satisfy the equal-length requirement, pad the shorter value with leading zero digits or, if the longer value begins with leading zero digits and a shorter result value is acceptable, strip them:

  1. mysql> SELECT _binary X'0040' | X'0001';
  2. +---------------------------+
  3. | _binary X'0040' | X'0001' |
  4. +---------------------------+
  5. |  A                        |
  6. +---------------------------+
  7. mysql> SELECT _binary X'40' | X'01';
  8. +-----------------------+
  9. | _binary X'40' | X'01' |
  10. +-----------------------+
  11. | A                     |
  12. +-----------------------+

Padding or stripping can also be accomplished using functions such as LPAD(), RPAD(), SUBSTR(), or CAST(). In such cases, the expression arguments are no longer all literals and _binary becomes unnecessary. Examples:

  1. mysql> SELECT LPAD(X'40', 2, X'00') | X'0001';
  2. +---------------------------------+
  3. | LPAD(X'40', 2, X'00') | X'0001' |
  4. +---------------------------------+
  5. |  A                              |
  6. +---------------------------------+
  7. mysql> SELECT X'40' | SUBSTR(X'0001', 2, 1);
  8. +-------------------------------+
  9. | X'40' | SUBSTR(X'0001', 2, 1) |
  10. +-------------------------------+
  11. | A                             |
  12. +-------------------------------+

Contents Haut

Binary String Bit-Operation Examples

The following example illustrates use of bit operations to extract parts of a UUID value, in this case, the timestamp and IEEE 802 node number. This technique requires bitmasks for each extracted part.

Convert the text UUID to the corresponding 16-byte binary value so that it can be manipulated using bit operations in binary-string context:

  1. mysql> SET @uuid = UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db');
  2. mysql> SELECT HEX(@uuid);
  3. +----------------------------------+
  4. | HEX(@uuid)                       |
  5. +----------------------------------+
  6. | 6CCD780CBABA102695645B8C656024DB |
  7. +----------------------------------+

Construct bitmasks for the timestamp and node number parts of the value. The timestamp comprises the first three parts (64 bits, bits 0 to 63) and the node number is the last part (48 bits, bits 80 to 127):

  1. mysql> SET @ts_mask = CAST(X'FFFFFFFFFFFFFFFF' AS BINARY(16));
  2. mysql> SET @node_mask = CAST(X'FFFFFFFFFFFF' AS BINARY(16)) >> 80;
  3. mysql> SELECT HEX(@ts_mask);
  4. +----------------------------------+
  5. | HEX(@ts_mask)                    |
  6. +----------------------------------+
  7. | FFFFFFFFFFFFFFFF0000000000000000 |
  8. +----------------------------------+
  9. mysql> SELECT HEX(@node_mask);
  10. +----------------------------------+
  11. | HEX(@node_mask)                  |
  12. +----------------------------------+
  13. | 00000000000000000000FFFFFFFFFFFF |
  14. +----------------------------------+

The CAST(... AS BINARY(16)) function is used here because the masks must be the same length as the UUID value against which they are applied. The same result can be produced using other functions to pad the masks to the required length:

  1. SET @ts_mask= RPAD(X'FFFFFFFFFFFFFFFF' , 16, X'00');
  2. SET @node_mask = LPAD(X'FFFFFFFFFFFF', 16, X'00') ;

Use the masks to extract the timestamp and node number parts:

  1. mysql> SELECT HEX(@uuid & @ts_mask) AS 'timestamp part';
  2. +----------------------------------+
  3. | timestamp part                   |
  4. +----------------------------------+
  5. | 6CCD780CBABA10260000000000000000 |
  6. +----------------------------------+
  7. mysql> SELECT HEX(@uuid & @node_mask) AS 'node part';
  8. +----------------------------------+
  9. | node part                        |
  10. +----------------------------------+
  11. | 000000000000000000005B8C656024DB |
  12. +----------------------------------+

The preceding example uses these bit operations: right shift (>>) and bitwise AND (&).

Note

UUID_TO_BIN() takes a flag that causes some bit rearrangement in the resulting binary UUID value. If you use that flag, modify the extraction masks accordingly.

The next example uses bit operations to extract the network and host parts of an IPv6 address. Suppose that the network part has a length of 80 bits. Then the host part has a length of 128 − 80 = 48 bits. To extract the network and host parts of the address, convert it to a binary string, then use bit operations in binary-string context.

Convert the text IPv6 address to the corresponding binary string:

  1. mysql> SET @ip = INET6_ATON('fe80::219:d1ff:fe91:1a72');

Define the network length in bits:

  1. mysql> SET @net_len = 80;

Construct network and host masks by shifting the all-ones address left or right. To do this, begin with the address ::, which is shorthand for all zeros, as you can see by converting it to a binary string like this:

  1. mysql> SELECT HEX(INET6_ATON('::')) AS 'all zeros';
  2. +----------------------------------+
  3. | all zeros                        |
  4. +----------------------------------+
  5. | 00000000000000000000000000000000 |
  6. +----------------------------------+

To produce the complementary value (all ones), use the ~ operator to invert the bits:

  1. mysql> SELECT HEX(~INET6_ATON('::')) AS 'all ones';
  2. +----------------------------------+
  3. | all ones                         |
  4. +----------------------------------+
  5. | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
  6. +----------------------------------+

Shift the all-ones value left or right to produce the network and host masks:

  1. mysql> SET @net_mask = ~INET6_ATON('::') << (128 - @net_len);
  2. mysql> SET @host_mask = ~INET6_ATON('::') >> @net_len;

Display the masks to verify that they cover the correct parts of the address:

  1. mysql> SELECT INET6_NTOA(@net_mask) AS 'network mask';
  2. +----------------------------+
  3. | network mask               |
  4. +----------------------------+
  5. | ffff:ffff:ffff:ffff:ffff:: |
  6. +----------------------------+
  7. mysql> SELECT INET6_NTOA(@host_mask) AS 'host mask';
  8. +------------------------+
  9. | host mask              |
  10. +------------------------+
  11. | ::ffff:255.255.255.255 |
  12. +------------------------+

Extract and display the network and host parts of the address:

  1. mysql> SET @net_part = @ip & @net_mask;
  2. mysql> SET @host_part = @ip & @host_mask;
  3. mysql> SELECT INET6_NTOA(@net_part) AS 'network part';
  4. +-----------------+
  5. | network part    |
  6. +-----------------+
  7. | fe80::219:0:0:0 |
  8. +-----------------+
  9. mysql> SELECT INET6_NTOA(@host_part) AS 'host part';
  10. +------------------+
  11. | host part        |
  12. +------------------+
  13. | ::d1ff:fe91:1a72 |
  14. +------------------+

The preceding example uses these bit operations: Complement (~), left shift (<<), and bitwise AND (&).

The remaining discussion provides details on argument handling for each group of bit operations, more information about literal-value handling in bit operations, and potential incompatibilities between MySQL 8.0 and older MySQL versions.

Contents Haut

Bitwise AND, OR, and XOR Operations

For &, |, and ^ bit operations, the result type depends on whether the arguments are evaluated as binary strings or numbers:

  • Binary-string evaluation occurs when the arguments have a binary string type, and at least one of them is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to unsigned 64-bit integers as necessary.

  • Binary-string evaluation produces a binary string of the same length as the arguments. If the arguments have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned 64-bit integer.

Examples of numeric evaluation:

  1. mysql> SELECT 64 | 1, X'40' | X'01';
  2. +--------+---------------+
  3. | 64 | 1 | X'40' | X'01' |
  4. +--------+---------------+
  5. |     65 |            65 |
  6. +--------+---------------+

Examples of binary-string evaluation:

  1. mysql> SELECT _binary X'40' | X'01';
  2. +-----------------------+
  3. | _binary X'40' | X'01' |
  4. +-----------------------+
  5. | A                     |
  6. +-----------------------+
  7. mysql> SET @var1 = X'40', @var2 = X'01';
  8. mysql> SELECT @var1 | @var2;
  9. +---------------+
  10. | @var1 | @var2 |
  11. +---------------+
  12. | A             |
  13. +---------------+

Contents Haut

Bitwise Complement and Shift Operations

For ~, <<, and >> bit operations, the result type depends on whether the bit argument is evaluated as a binary string or number:

  • Binary-string evaluation occurs when the bit argument has a binary string type, and is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to an unsigned 64-bit integer as necessary.

  • Binary-string evaluation produces a binary string of the same length as the bit argument. Numeric evaluation produces an unsigned 64-bit integer.

For shift operations, bits shifted off the end of the value are lost without warning, regardless of the argument type. In particular, if the shift count is greater or equal to the number of bits in the bit argument, all bits in the result are 0.

Examples of numeric evaluation:

  1. mysql> SELECT ~0, 64 << 2, X'40' << 2;
  2. +----------------------+---------+------------+
  3. | ~0                   | 64 << 2 | X'40' << 2 |
  4. +----------------------+---------+------------+
  5. | 18446744073709551615 |     256 |        256 |
  6. +----------------------+---------+------------+

Examples of binary-string evaluation:

  1. mysql> SELECT HEX(_binary X'1111000022220000' >> 16);
  2. +----------------------------------------+
  3. | HEX(_binary X'1111000022220000' >> 16) |
  4. +----------------------------------------+
  5. | 0000111100002222                       |
  6. +----------------------------------------+
  7. mysql> SELECT HEX(_binary X'1111000022220000' << 16);
  8. +----------------------------------------+
  9. | HEX(_binary X'1111000022220000' << 16) |
  10. +----------------------------------------+
  11. | 0000222200000000                       |
  12. +----------------------------------------+
  13. mysql> SET @var1 = X'F0F0F0F0';
  14. mysql> SELECT HEX(~@var1);
  15. +-------------+
  16. | HEX(~@var1) |
  17. +-------------+
  18. | 0F0F0F0F    |
  19. +-------------+

Contents Haut

BIT_COUNT() Operations

The BIT_COUNT() function always returns an unsigned 64-bit integer, or NULL if the argument is NULL.

  1. mysql> SELECT BIT_COUNT(127);
  2. +----------------+
  3. | BIT_COUNT(127) |
  4. +----------------+
  5. |              7 |
  6. +----------------+
  7. mysql> SELECT BIT_COUNT(b'010101'), BIT_COUNT(_binary b'010101');
  8. +----------------------+------------------------------+
  9. | BIT_COUNT(b'010101') | BIT_COUNT(_binary b'010101') |
  10. +----------------------+------------------------------+
  11. |                    3 |                            3 |
  12. +----------------------+------------------------------+

Contents Haut

BIT_AND(), BIT_OR(), and BIT_XOR() Operations

For the BIT_AND(), BIT_OR(), and BIT_XOR() bit functions, the result type depends on whether the function argument values are evaluated as binary strings or numbers:

  • Binary-string evaluation occurs when the argument values have a binary string type, and the argument is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument value conversion to unsigned 64-bit integers as necessary.

  • Binary-string evaluation produces a binary string of the same length as the argument values. If argument values have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs. If the argument size exceeds 511 bytes, an ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned 64-bit integer.

NULL values do not affect the result unless all values are NULL. In that case, the result is a neutral value having the same length as the length of the argument values (all bits 1 for BIT_AND(), all bits 0 for BIT_OR(), and BIT_XOR()).

Example:

  1. mysql> CREATE TABLE t (group_id INT, a VARBINARY(6));
  2. mysql> INSERT INTO t VALUES (1, NULL);
  3. mysql> INSERT INTO t VALUES (1, NULL);
  4. mysql> INSERT INTO t VALUES (2, NULL);
  5. mysql> INSERT INTO t VALUES (2, X'1234');
  6. mysql> INSERT INTO t VALUES (2, X'FF34');
  7. mysql> SELECT HEX(BIT_AND(a)), HEX(BIT_OR(a)), HEX(BIT_XOR(a))
  8.        FROM t GROUP BY group_id;
  9. +-----------------+----------------+-----------------+
  10. | HEX(BIT_AND(a)) | HEX(BIT_OR(a)) | HEX(BIT_XOR(a)) |
  11. +-----------------+----------------+-----------------+
  12. | FFFFFFFFFFFF    | 000000000000   | 000000000000    |
  13. | 1234            | FF34           | ED00            |
  14. +-----------------+----------------+-----------------+

Special Handling of Hexadecimal Literals, Bit Literals, and NULL Literals

For backward compatibility, MySQL 8.0 evaluates bit operations in numeric context when all bit arguments are hexadecimal literals, bit literals, or NULL literals. That is, bit operations on binary-string bit arguments do not use binary-string evaluation if all bit arguments are unadorned hexadecimal literals, bit literals, or NULL literals. (This does not apply to such literals if they are written with a _binary introducer, BINARY operator, or other way of specifying them explicitly as binary strings.)

The literal handling just described is the same as prior to MySQL 8.0. Examples:

  • These bit operations evaluate the literals in numeric context and produce a BIGINT result:

    1. b'0001' | b'0010'
    2. X'0008' << 8
  • These bit operations evaluate NULL in numeric context and produce a BIGINT result that has a NULL value:

    1. NULL >> 4

In MySQL 8.0, you can cause those operations to evaluate the arguments in binary-string context by indicating explicitly that at least one argument is a binary string:

  1. _binary b'0001' | b'0010'
  2. _binary X'0008' << 8

The result of the last two expressions is NULL, just as without the BINARY operator, but the data type of the result is a binary string type rather than an integer type.

Contents Haut

Bit-Operation Incompatibilities with MySQL 5.7

Because bit operations can handle binary string arguments natively in MySQL 8.0, some expressions produce a different result in MySQL 8.0 than in 5.7. The five problematic expression types to watch out for are:

  1. nonliteral_binary { & | ^ } binary
  2. binary  { & | ^ } nonliteral_binary
  3. nonliteral_binary { << >> } anything
  4. ~ nonliteral_binary
  5. AGGR_BIT_FUNC(nonliteral_binary)

Those expressions return BIGINT in MySQL 5.7, binary string in 8.0.

Explanation of notation:

  • { op1 op2 ... }: List of operators that apply to the given expression type.

  • binary: Any kind of binary string argument, including a hexadecimal literal, bit literal, or NULL literal.

  • nonliteral_binary: An argument that is a binary string value other than a hexadecimal literal, bit literal, or NULL literal.

  • AGGR_BIT_FUNC: An aggregate function that takes bit-value arguments: BIT_AND(), BIT_OR(), BIT_XOR().

For information about how to prepare in MySQL 5.7 for potential incompatibilities between MySQL 5.7 and 8.0, see Bit Functions and Operators, in MySQL 5.7 Reference Manual.

The following list describes available bit functions and operators:

  • |

    Bitwise OR.

    The result type depends on whether the arguments are evaluated as binary strings or numbers:

    • Binary-string evaluation occurs when the arguments have a binary string type, and at least one of them is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to unsigned 64-bit integers as necessary.

    • Binary-string evaluation produces a binary string of the same length as the arguments. If the arguments have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned 64-bit integer.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 29 | 15;
    2.         -> 31
    3. mysql> SELECT _binary X'40404040' | X'01020304';
    4.         -> 'ABCD'
  • &

    Bitwise AND.

    The result type depends on whether the arguments are evaluated as binary strings or numbers:

    • Binary-string evaluation occurs when the arguments have a binary string type, and at least one of them is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to unsigned 64-bit integers as necessary.

    • Binary-string evaluation produces a binary string of the same length as the arguments. If the arguments have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned 64-bit integer.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 29 & 15;
    2.         -> 13
    3. mysql> SELECT HEX(_binary X'FF' & b'11110000');
    4.         -> 'F0'
  • ^

    Bitwise XOR.

    The result type depends on whether the arguments are evaluated as binary strings or numbers:

    • Binary-string evaluation occurs when the arguments have a binary string type, and at least one of them is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to unsigned 64-bit integers as necessary.

    • Binary-string evaluation produces a binary string of the same length as the arguments. If the arguments have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned 64-bit integer.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 1 ^ 1;
    2.         -> 0
    3. mysql> SELECT 1 ^ 0;
    4.         -> 1
    5. mysql> SELECT 11 ^ 3;
    6.         -> 8
    7. mysql> SELECT HEX(_binary X'FEDC' ^ X'1111');
    8.         -> 'EFCD'
  • <<

    Shifts a longlong (BIGINT) number or binary string to the left.

    The result type depends on whether the bit argument is evaluated as a binary string or number:

    • Binary-string evaluation occurs when the bit argument has a binary string type, and is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to an unsigned 64-bit integer as necessary.

    • Binary-string evaluation produces a binary string of the same length as the bit argument. Numeric evaluation produces an unsigned 64-bit integer.

    Bits shifted off the end of the value are lost without warning, regardless of the argument type. In particular, if the shift count is greater or equal to the number of bits in the bit argument, all bits in the result are 0.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 1 << 2;
    2.         -> 4
    3. mysql> SELECT HEX(_binary X'00FF00FF00FF' << 8);
    4.         -> 'FF00FF00FF00'
  • >>

    Shifts a longlong (BIGINT) number or binary string to the right.

    The result type depends on whether the bit argument is evaluated as a binary string or number:

    • Binary-string evaluation occurs when the bit argument has a binary string type, and is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to an unsigned 64-bit integer as necessary.

    • Binary-string evaluation produces a binary string of the same length as the bit argument. Numeric evaluation produces an unsigned 64-bit integer.

    Bits shifted off the end of the value are lost without warning, regardless of the argument type. In particular, if the shift count is greater or equal to the number of bits in the bit argument, all bits in the result are 0.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 4 >> 2;
    2.         -> 1
    3. mysql> SELECT HEX(_binary X'00FF00FF00FF' >> 8);
    4.         -> '0000FF00FF00'
  • ~

    Invert all bits.

    The result type depends on whether the bit argument is evaluated as a binary string or number:

    • Binary-string evaluation occurs when the bit argument has a binary string type, and is not a hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument conversion to an unsigned 64-bit integer as necessary.

    • Binary-string evaluation produces a binary string of the same length as the bit argument. Numeric evaluation produces an unsigned 64-bit integer.

    For more information, see the introductory discussion in this section.

    1. mysql> SELECT 5 & ~1;
    2.         -> 4
    3. mysql> SELECT HEX(~X'0000FFFF1111EEEE');
    4.         -> 'FFFF0000EEEE1111'
  • BIT_COUNT(N)

    Returns the number of bits that are set in the argument N as an unsigned 64-bit integer, or NULL if the argument is NULL.

    1. mysql> SELECT BIT_COUNT(64), BIT_COUNT(BINARY 64);
    2.         -> 1, 7
    3. mysql> SELECT BIT_COUNT('64'), BIT_COUNT(_binary '64');
    4.         -> 1, 7
    5. mysql> SELECT BIT_COUNT(X'40'), BIT_COUNT(_binary X'40');
    6.         -> 1, 1

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-bit-functions.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut