def bin(x, l=24, d=8, reverse=False):
   """returns the binary representation of an 'l'-bit integer 'x' as a string

   Leading zeros are printed, blanks are inserted between 'd'-bit blocks.
   'd' may also be a list of bit positions where to insert blanks. If
   'reverse' is True, the string starts with bit 0 (which is fine for bit
   vectors), otherwise it starts with bit l-1 (as usual for integers). 
   """
   s = ""
   if not isinstance(d, list):  d=range(d,l,d)
   if reverse:
      for i in range(l):
         if x & (1<<i): s += '1'
         else: s += '0'
         if i+1 in d: s += ' '
   else:
      for i in range(l-1,-1,-1):
         if x & (1<<i): s += '1'
         else: s += '0'
         if i in d: s += ' '
   return s