Is BIT_LENGTH() useful?

mysql [localhost] {msandbox} ((none)) > select length(crc32(3)) * 8, bit_length(crc32(3));
+----------------------+----------------------+
| length(crc32(3)) * 8 | bit_length(crc32(3)) |
+----------------------+----------------------+
|                   80 |                   80 |
+----------------------+----------------------+
1 row in set (0.00 sec)

6 Responses to “Is BIT_LENGTH() useful?”

  1. Robert Wultsch says:

    I have never seen bit_length outside of man pages.

  2. Rob says:

    Does it change with a UTF-16 string?

  3. Antony Curtis says:

    Try using it with a UTF8 string which has some multi-byte characters. LENGTH should return number of characters, BIT_LENGTH would include all the bytes which are used to encode it.
    So I would say: Definitely useful.

  4. Shlomi Noach says:

    @Antony:

    LENGTH() returns the number of bytes consumed, not the number of characters.
    To get the number of characters, you have CHAR_LENGTH().

    With non-ascii characters, and on utf8 charset, it will follow that LENGTH(t) > CHAR_LENGTH(t)

    (BTW, BIT_LENGTH is still LENGTH*8 in this case)

  5. Have never used it myself, and have never seen it in use.

  6. PB says:

    I have never seen bit_length outside of man pages.

Leave a Reply