def FormatAsBits(output_and_bits):
  """ Takes as input a tuple representing (array_of_bytes, number_of_bits),
  and formats it as binary, with byte-boundaries marked"""
  (output, bits) = output_and_bits
  retval = []
  if not bits:
    total_bits = len(output) * 8
  elif bits % 8:
    total_bits = (len(output) - 1) * 8 + (bits % 8)
  else:
    total_bits = len(output) * 8
  idx = 0
  while total_bits >= 8:
    c = output[idx]
    idx += 1
    retval.append('|')
    retval.append('{0:08b}'.format(c))
    total_bits -= 8
  if (bits % 8) != 0:
    retval.append('|')
    retval.append('{0:08b}'.format(output[idx])[0:(bits % 8)])
  retval.extend([' [%d]' % bits])
  return ''.join(retval)