import struct


def encode_len(length: int) -> bytes:
    """Encodes the specified length as 1,2 or 5 bytes of
    RDB specific length encoded byte.

    - For values less than 64 (i.e two MSBs zero - encode directly in the byte)

    - For values less than 16384 use two bytes, leading MSBs are 01 followed by
      14 bits encoding the value

    - For values less than (2^32 - 1) use 5 bytes, leading MSBs are 10. Length
      encoded only in the lowest 32 bits.
    """
    if length > (2**32 - 1):
        raise ValueError("Cannot encode item of length {} as it is greater than 2^32 -1"
                         .format(length))
    if length < 64:
        return struct.pack('B', length)
    if length < 16384:
        return struct.pack(">H", 0x4000 + length)
    return struct.pack('>Q', 0x8000000000 + length)[3:]