Rechercher dans le manuel MySQL
12.12 Bit Functions and Operators
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:
This statement performs to-number conversions on the string arguments (
'127'
to127
, and so forth) before performing the same operations as the first statement and producing the same results: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:
- +---------------+------------+------------------+
- +---------------+------------+------------------+
- | 255 | 512 | 4 |
- +---------------+------------+------------------+
Handling of bit-value literals in bit operations is similar to hexadecimal literals (that is, as numbers).
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):
- +----------------------------------------------------------+
- +----------------------------------------------------------+
- | 6CCD780CBABA102695645B8C656024DB |
- +----------------------------------------------------------+
- +---------------------------------------------+
- +---------------------------------------------+
- | FE800000000000000219D1FFFE911A72 |
- +---------------------------------------------+
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:
These bit operations evaluate the hexadecimal literals and bit literals as binary strings, due to the
_binary
introducer:- +-----------------------+-----------------------------------+
- +-----------------------+-----------------------------------+
- | A | A |
- +-----------------------+-----------------------------------+
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:
- +---------------------------------------------------+
- +---------------------------------------------------+
- | ABCDEFGH |
- +---------------------------------------------------+
There are several ways to refer to a hexadecimal literal or bit literal in a bit operation to cause binary-string evaluation:
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:
- +-----------+-----------+
- | @v1 | @v2 | @v3 & @v4 |
- +-----------+-----------+
- | A | A |
- +-----------+-----------+
In binary-string context, bitwise operation arguments must have
the same length or an
ER_INVALID_BITWISE_OPERANDS_SIZE
error occurs:
- 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:
- +---------------------------+
- +---------------------------+
- | A |
- +---------------------------+
- +-----------------------+
- +-----------------------+
- | A |
- +-----------------------+
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:
- +---------------------------------+
- +---------------------------------+
- | A |
- +---------------------------------+
- +-------------------------------+
- +-------------------------------+
- | A |
- +-------------------------------+
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:
- +----------------------------------+
- +----------------------------------+
- | 6CCD780CBABA102695645B8C656024DB |
- +----------------------------------+
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):
- +----------------------------------+
- +----------------------------------+
- | FFFFFFFFFFFFFFFF0000000000000000 |
- +----------------------------------+
- +----------------------------------+
- +----------------------------------+
- | 00000000000000000000FFFFFFFFFFFF |
- +----------------------------------+
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:
Use the masks to extract the timestamp and node number parts:
- +----------------------------------+
- +----------------------------------+
- | 6CCD780CBABA10260000000000000000 |
- +----------------------------------+
- +----------------------------------+
- | node part |
- +----------------------------------+
- | 000000000000000000005B8C656024DB |
- +----------------------------------+
The preceding example uses these bit operations: right shift
(>>
)
and bitwise AND
(&
).
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:
Define the network length in bits:
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:
- +----------------------------------+
- +----------------------------------+
- | 00000000000000000000000000000000 |
- +----------------------------------+
To produce the complementary value (all ones), use the
~
operator to invert the bits:
- +----------------------------------+
- +----------------------------------+
- | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
- +----------------------------------+
Shift the all-ones value left or right to produce the network and host masks:
Display the masks to verify that they cover the correct parts of the address:
- +----------------------------+
- | network mask |
- +----------------------------+
- | ffff:ffff:ffff:ffff:ffff:: |
- +----------------------------+
- +------------------------+
- | host mask |
- +------------------------+
- | ::ffff:255.255.255.255 |
- +------------------------+
Extract and display the network and host parts of the address:
- +-----------------+
- | network part |
- +-----------------+
- | fe80::219:0:0:0 |
- +-----------------+
- +------------------+
- | host part |
- +------------------+
- | ::d1ff:fe91:1a72 |
- +------------------+
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.
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:
- +--------+---------------+
- +--------+---------------+
- | 65 | 65 |
- +--------+---------------+
Examples of binary-string evaluation:
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:
- +----------------------+---------+------------+
- +----------------------+---------+------------+
- | 18446744073709551615 | 256 | 256 |
- +----------------------+---------+------------+
Examples of binary-string evaluation:
- +----------------------------------------+
- +----------------------------------------+
- | 0000111100002222 |
- +----------------------------------------+
- +----------------------------------------+
- +----------------------------------------+
- | 0000222200000000 |
- +----------------------------------------+
- +-------------+
- +-------------+
- | 0F0F0F0F |
- +-------------+
The BIT_COUNT()
function always
returns an unsigned 64-bit integer, or NULL
if the argument is NULL
.
- +----------------+
- +----------------+
- | 7 |
- +----------------+
- +----------------------+------------------------------+
- +----------------------+------------------------------+
- | 3 | 3 |
- +----------------------+------------------------------+
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, anER_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:
- +-----------------+----------------+-----------------+
- +-----------------+----------------+-----------------+
- | FFFFFFFFFFFF | 000000000000 | 000000000000 |
- | 1234 | FF34 | ED00 |
- +-----------------+----------------+-----------------+
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:- b'0001' | b'0010'
These bit operations evaluate
NULL
in numeric context and produce aBIGINT
result that has aNULL
value:
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:
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.
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:
- nonliteral_binary { << >> } anything
- ~ nonliteral_binary
- AGGR_BIT_FUNC(nonliteral_binary)
Those expressions return BIGINT
in MySQL 5.7, binary string in 8.0.
Explanation of notation:
{
: List of operators that apply to the given expression type.op1
op2
... }binary
: Any kind of binary string argument, including a hexadecimal literal, bit literal, orNULL
literal.nonliteral_binary
: An argument that is a binary string value other than a hexadecimal literal, bit literal, orNULL
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.
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.
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.
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.
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.
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.
Returns the number of bits that are set in the argument
N
as an unsigned 64-bit integer, orNULL
if the argument isNULL
.
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
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.