code: def bitstring_to_bytes(bitstr):
      
      bitlist = list(bitstr)
      bits_missing = (8 - len(bitlist) % 8) % 8
      bitlist = [0]*bits_missing + bitlist # pad with 0 bits to a multiple of 8
      result = bytearray()
      for i in range(0, len(bitlist), 8):
          byte = 0
          for j in range(8):
              byte = (byte << 1) | bitlist[i+j]
          result.append(byte)
      return bytes(result)
docs: Converts a pyasn1 univ.BitString instance to byte sequence of type 'bytes'.
      The bit string is interpreted big-endian and is left-padded with 0 bits to form a multiple of 8.