code: def __allocateBits(self):
          
          if self._bit_count < 0:
              raise Exception( "A margin cannot request negative number of bits" )
          if self._bit_count == 0:
              return
  
          # Build a list of occupied ranges
          margins = self._qpart.getMargins()
  
          occupiedRanges = []
          for margin in margins:
              bitRange = margin.getBitRange()
              if bitRange is not None:
                  # pick the right position
                  added = False
                  for index in range( len( occupiedRanges ) ):
                      r = occupiedRanges[ index ]
                      if bitRange[ 1 ] < r[ 0 ]:
                          occupiedRanges.insert(index, bitRange)
                          added = True
                          break
                  if not added:
                      occupiedRanges.append(bitRange)
  
          vacant = 0
          for r in occupiedRanges:
              if r[ 0 ] - vacant >= self._bit_count:
                  self._bitRange = (vacant, vacant + self._bit_count - 1)
                  return
              vacant = r[ 1 ] + 1
          # Not allocated, i.e. grab the tail bits
          self._bitRange = (vacant, vacant + self._bit_count - 1)
docs: Allocates the bit range depending on the required bit count