MySQL 8.0 Reference Manual Including MySQL NDB Cluster 8.0

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 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 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:

mysql>

+-----------+----------+---------------+ | 127 | 128 | 128 << 2 | BIT_COUNT(15) | +-----------+----------+---------------+ | 255 | 512 | 4 | +-----------+----------+---------------+`SELECT 127 | 128, 128 << 2, BIT_COUNT(15);`

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:mysql>

+---------------+------------+-----------------+ | '127' | '128' | '128' << 2 | BIT_COUNT('15') | +---------------+------------+-----------------+ | 255 | 512 | 4 | +---------------+------------+-----------------+`SELECT '127' | '128', '128' << 2, BIT_COUNT('15');`

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:

mysql>

+---------------+------------+------------------+ | X'7F' | X'80' | X'80' << 2 | BIT_COUNT(X'0F') | +---------------+------------+------------------+ | 255 | 512 | 4 | +---------------+------------+------------------+`SELECT X'7F' | X'80', X'80' << 2, BIT_COUNT(X'0F');`

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):

mysql>+----------------------------------------------------------+ | HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db')) | +----------------------------------------------------------+ | 6CCD780CBABA102695645B8C656024DB | +----------------------------------------------------------+ mysql>`SELECT HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'));`

+---------------------------------------------+ | HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72')) | +---------------------------------------------+ | FE800000000000000219D1FFFE911A72 | +---------------------------------------------+`SELECT HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72'));`

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:

mysql>

+---------------+---------------------------+ | X'40' | X'01' | b'11110001' & b'01001111' | +---------------+---------------------------+ | 65 | 65 | +---------------+---------------------------+`SELECT X'40' | X'01', b'11110001' & b'01001111';`

These bit operations evaluate the hexadecimal literals and bit literals as binary strings, due to the

`_binary`

introducer:mysql>

+-----------------------+-----------------------------------+ | _binary X'40' | X'01' | b'11110001' & _binary b'01001111' | +-----------------------+-----------------------------------+ | A | A | +-----------------------+-----------------------------------+`SELECT _binary X'40' | X'01', b'11110001' & _binary b'01001111';`

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:

mysql>+---------------------------------------------------+ | _binary X'4040404040404040' | X'0102030405060708' | +---------------------------------------------------+ | ABCDEFGH | +---------------------------------------------------+`SELECT _binary X'4040404040404040' | X'0102030405060708';`

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

_binaryBINARY`literal`

CAST(`literal`

AS BINARY)`literal`

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:

mysql>mysql>`SET @v1 = X'40', @v2 = X'01', @v3 = b'11110001', @v4 = b'01001111';`

+-----------+-----------+ | @v1 | @v2 | @v3 & @v4 | +-----------+-----------+ | A | A | +-----------+-----------+`SELECT @v1 | @v2, @v3 & @v4;`

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

error occurs:

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

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:

mysql>+---------------------------+ | _binary X'0040' | X'0001' | +---------------------------+ | A | +---------------------------+ mysql>`SELECT _binary X'0040' | X'0001';`

+-----------------------+ | _binary X'40' | X'01' | +-----------------------+ | A | +-----------------------+`SELECT _binary X'40' | X'01';`

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:

mysql>+---------------------------------+ | LPAD(X'40', 2, X'00') | X'0001' | +---------------------------------+ | A | +---------------------------------+ mysql>`SELECT LPAD(X'40', 2, X'00') | X'0001';`

+-------------------------------+ | X'40' | SUBSTR(X'0001', 2, 1) | +-------------------------------+ | A | +-------------------------------+`SELECT X'40' | SUBSTR(X'0001', 2, 1);`

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:

mysql>mysql>`SET @uuid = UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db');`

+----------------------------------+ | HEX(@uuid) | +----------------------------------+ | 6CCD780CBABA102695645B8C656024DB | +----------------------------------+`SELECT HEX(@uuid);`

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):

mysql>mysql>`SET @ts_mask = CAST(X'FFFFFFFFFFFFFFFF' AS BINARY(16));`

mysql>`SET @node_mask = CAST(X'FFFFFFFFFFFF' AS BINARY(16)) >> 80;`

+----------------------------------+ | HEX(@ts_mask) | +----------------------------------+ | FFFFFFFFFFFFFFFF0000000000000000 | +----------------------------------+ mysql>`SELECT HEX(@ts_mask);`

+----------------------------------+ | HEX(@node_mask) | +----------------------------------+ | 00000000000000000000FFFFFFFFFFFF | +----------------------------------+`SELECT HEX(@node_mask);`

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:

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

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

mysql>+----------------------------------+ | timestamp part | +----------------------------------+ | 6CCD780CBABA10260000000000000000 | +----------------------------------+ mysql>`SELECT HEX(@uuid & @ts_mask) AS 'timestamp part';`

+----------------------------------+ | node part | +----------------------------------+ | 000000000000000000005B8C656024DB | +----------------------------------+`SELECT HEX(@uuid & @node_mask) AS 'node part';`

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:

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

Define the network length in bits:

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:

mysql>+----------------------------------+ | all zeros | +----------------------------------+ | 00000000000000000000000000000000 | +----------------------------------+`SELECT HEX(INET6_ATON('::')) AS 'all zeros';`

To produce the complementary value (all ones), use the
`~`

operator to invert the bits:

mysql>+----------------------------------+ | all ones | +----------------------------------+ | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | +----------------------------------+`SELECT HEX(~INET6_ATON('::')) AS 'all ones';`

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

mysql>mysql>`SET @net_mask = ~INET6_ATON('::') << (128 - @net_len);`

`SET @host_mask = ~INET6_ATON('::') >> @net_len;`

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

mysql>+----------------------------+ | network mask | +----------------------------+ | ffff:ffff:ffff:ffff:ffff:: | +----------------------------+ mysql>`SELECT INET6_NTOA(@net_mask) AS 'network mask';`

+------------------------+ | host mask | +------------------------+ | ::ffff:255.255.255.255 | +------------------------+`SELECT INET6_NTOA(@host_mask) AS 'host mask';`

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

mysql>mysql>`SET @net_part = @ip & @net_mask;`

mysql>`SET @host_part = @ip & @host_mask;`

+-----------------+ | network part | +-----------------+ | fe80::219:0:0:0 | +-----------------+ mysql>`SELECT INET6_NTOA(@net_part) AS 'network part';`

+------------------+ | host part | +------------------+ | ::d1ff:fe91:1a72 | +------------------+`SELECT INET6_NTOA(@host_part) AS 'host part';`

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:

mysql>+--------+---------------+ | 64 | 1 | X'40' | X'01' | +--------+---------------+ | 65 | 65 | +--------+---------------+`SELECT 64 | 1, X'40' | X'01';`

Examples of binary-string evaluation:

mysql>+-----------------------+ | _binary X'40' | X'01' | +-----------------------+ | A | +-----------------------+ mysql>`SELECT _binary X'40' | X'01';`

mysql>`SET @var1 = X'40', @var2 = X'01';`

+---------------+ | @var1 | @var2 | +---------------+ | A | +---------------+`SELECT @var1 | @var2;`

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:

mysql>+----------------------+---------+------------+ | ~0 | 64 << 2 | X'40' << 2 | +----------------------+---------+------------+ | 18446744073709551615 | 256 | 256 | +----------------------+---------+------------+`SELECT ~0, 64 << 2, X'40' << 2;`

Examples of binary-string evaluation:

mysql>+----------------------------------------+ | HEX(_binary X'1111000022220000' >> 16) | +----------------------------------------+ | 0000111100002222 | +----------------------------------------+ mysql>`SELECT HEX(_binary X'1111000022220000' >> 16);`

+----------------------------------------+ | HEX(_binary X'1111000022220000' << 16) | +----------------------------------------+ | 0000222200000000 | +----------------------------------------+ mysql>`SELECT HEX(_binary X'1111000022220000' << 16);`

mysql>`SET @var1 = X'F0F0F0F0';`

+-------------+ | HEX(~@var1) | +-------------+ | 0F0F0F0F | +-------------+`SELECT HEX(~@var1);`

The `BIT_COUNT()`

function always
returns an unsigned 64-bit integer, or `NULL`

if the argument is `NULL`

.

mysql>+----------------+ | BIT_COUNT(127) | +----------------+ | 7 | +----------------+ mysql>`SELECT BIT_COUNT(127);`

+----------------------+------------------------------+ | BIT_COUNT(b'010101') | BIT_COUNT(_binary b'010101') | +----------------------+------------------------------+ | 3 | 3 | +----------------------+------------------------------+`SELECT BIT_COUNT(b'010101'), BIT_COUNT(_binary b'010101');`

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:

mysql>mysql>`CREATE TABLE t (group_id INT, a VARBINARY(6));`

mysql>`INSERT INTO t VALUES (1, NULL);`

mysql>`INSERT INTO t VALUES (1, NULL);`

mysql>`INSERT INTO t VALUES (2, NULL);`

mysql>`INSERT INTO t VALUES (2, X'1234');`

mysql>`INSERT INTO t VALUES (2, X'FF34');`

`SELECT HEX(BIT_AND(a)), HEX(BIT_OR(a)), HEX(BIT_XOR(a))`

+-----------------+----------------+-----------------+ | HEX(BIT_AND(a)) | HEX(BIT_OR(a)) | HEX(BIT_XOR(a)) | +-----------------+----------------+-----------------+ | FFFFFFFFFFFF | 000000000000 | 000000000000 | | 1234 | FF34 | ED00 | +-----------------+----------------+-----------------+`FROM t GROUP BY group_id;`

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' X'0008' << 8

These bit operations evaluate

`NULL`

in numeric context and produce a`BIGINT`

result that has a`NULL`

value:NULL & NULL 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:

_binary b'0001' | b'0010' _binary X'0008' << 8 BINARY NULL & NULL BINARY NULL >> 4

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`

`binary`

{ & | ^ }`binary`

`nonliteral_binary`

{ << >> }`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`

: Any kind of binary string argument, including a hexadecimal literal, bit literal, or`binary`

`NULL`

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

`NULL`

literal.: An aggregate function that takes bit-value arguments:`AGGR_BIT_FUNC`

`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.

mysql>

-> 31 mysql>`SELECT 29 | 15;`

-> 'ABCD'`SELECT _binary X'40404040' | X'01020304';`

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.

mysql>

-> 13 mysql>`SELECT 29 & 15;`

-> 'F0'`SELECT HEX(_binary X'FF' & b'11110000');`

Bitwise XOR.

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

`NULL`

literal. Numeric evaluation occurs otherwise, with argument conversion to unsigned 64-bit integers as necessary.`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.

mysql>

-> 0 mysql>`SELECT 1 ^ 1;`

-> 1 mysql>`SELECT 1 ^ 0;`

-> 8 mysql>`SELECT 11 ^ 3;`

-> 'EFCD'`SELECT HEX(_binary X'FEDC' ^ X'1111');`

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.

mysql>

-> 4 mysql>`SELECT 1 << 2;`

-> 'FF00FF00FF00'`SELECT HEX(_binary X'00FF00FF00FF' << 8);`

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.

mysql>

-> 1 mysql>`SELECT 4 >> 2;`

-> '0000FF00FF00'`SELECT HEX(_binary X'00FF00FF00FF' >> 8);`

Invert all bits.

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

`NULL`

literal. Numeric evaluation occurs otherwise, with argument conversion to an unsigned 64-bit integer as necessary.

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

mysql>

-> 4 mysql>`SELECT 5 & ~1;`

-> 'FFFF0000EEEE1111'`SELECT HEX(~X'0000FFFF1111EEEE');`

Returns the number of bits that are set in the argument

as an unsigned 64-bit integer, or`N`

`NULL`

if the argument is`NULL`

.mysql>

-> 1, 7 mysql>`SELECT BIT_COUNT(64), BIT_COUNT(BINARY 64);`

-> 1, 7 mysql>`SELECT BIT_COUNT('64'), BIT_COUNT(_binary '64');`

-> 1, 1`SELECT BIT_COUNT(X'40'), BIT_COUNT(_binary X'40');`