AnalyticDB for MySQL supports the following bit functions and operators.

  • BIT_COUNT: This function converts an argument to a binary value, and then returns the number of bits that are set to 1 in the value.
  • &: bitwise AND.
  • ~: inverts all bits.
  • |: bitwise OR.
  • ^: bitwise XOR.
  • >>(BITWISE_RIGHT_SHIFT): shifts a value to the right.
  • <<(BITWISE_LEFT_SHIFT): shifts a value to the left.

BIT_COUNT

bit_count(bigint x)
bit_count(double x)
bit_count(varchar x)
  • Description: converts an argument to a binary value, and then returns the number of bits that are set to 1 in the value.
  • Return value type: BIGINT.
  • Example:
    select bit_count(2);
    +--------------+
    | bit_count(2) |
    +--------------+
    |            1 |
    select bit_count(pi());
    +-----------------+
    | bit_count(pi()) |
    +-----------------+
    |               2 |
    select bit_count('123');
    +------------------+| 
    bit_count('123') |
    +------------------+
    |                6 | 

&

  • Description: This function is used for bitwise AND.
  • Return value type: BIGINT.
  • Example:
     select 12 & 15;
    +---------------------+
    | bitwise_and(12, 15) |
    +---------------------+
    |                  12 |

~

  • Description: This function inverts all bits.
  • Return value type: BIGINT.
  • Example:
    select 2 & ~1;
    +--------------------------------+
    | bitwise_and(2, bitwise_not(1)) |
    +--------------------------------+
    |                              2 |

|

  • Description: This function is used for bitwise OR.
  • Return value type: BIGINT.
  • Example:
    select 29 | 15;
    +--------------------+
    | bitwise_or(29, 15) |
    +--------------------+
    |                 31 

^

  • Description: This function is used for bitwise XOR.
  • Return value type: BIGINT.
  • Example:
    select 1 ^ 10;
    +--------------------+
    | bitwise_xor(1, 10) |
    +--------------------+
    |                 11 | 

>>(BITWISE_RIGHT_SHIFT)

bitwise_right_shift(double x, double y) 
bitwise_right_shift(varchar x, varchar y)
bitwise_right_shift(bigint x, bigint y)
  • Description: This function shifts a value to the right.
  • Return value type: BIGINT.
  • Example:
    select 3 >> 2;
    +---------------------------+
    | bitwise_right_shift(3, 2) |
    +---------------------------+
    |                         0 |
    select 3.4 >> 23.2;
    +--------------------------------+
    | bitwise_right_shift(3.4, 23.2) |
    +--------------------------------+
    |                              0 |

<<(BITWISE_LEFT_SHIFT)

bitwise_left_shift(double x, double y) 
bitwise_left_shift(varchar x, varchar y)
bitwise_left_shift(bigint x, bigint y)
  • Description: This function shifts a value to the left.
  • Return value type: BIGINT.
  • Example:
    SELECT 3 << 2;
    +--------------------------+
    | bitwise_left_shift(3, 2) |
    +--------------------------+
    |                       12 |
    select '3' << '2';
    +------------------------------+
    | bitwise_left_shift('3', '2') |
    +------------------------------+
    |                           12 |
    select 3.4 << 23.2;
    +-------------------------------+
    | bitwise_left_shift(3.4, 23.2) |
    +-------------------------------+
    |                      25165824 |