content_image/810240.jpg
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> SELECT HEX(@uuid & @ts_mask) AS ’timestamp part’; +----------------------------------+ | timestamp part | +----------------------------------+ | 6CCD780CBABA10260000000000000000 | +----------------------------------+ mysql> SELECT HEX(@uuid & @node_mask) AS ’node part’; +----------------------------------+ | node part | +----------------------------------+ | 000000000000000000005B8C656024DB | +----------------------------------+ 

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

<img src='content_image/810240.jpg'>

## 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> SELECT HEX(INET6_ATON(’::’)) AS ’all zeros’; +----------------------------------+ | all zeros | +----------------------------------+ | 00000000000000000000000000000000 | +----------------------------------+ 

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

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

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

