text: class Bits(object):
  
      MAX_BITS = 32
  
      def flip_bit(self, num):
          if num is None:
              raise TypeError('num cannot be None')
          if num == -1:
              return self.MAX_BITS
          if num == 0:
              return 1
          seen = self._build_seen_list(num)
          max_result = 0
          looking_for = 0
          for index, count in enumerate(seen):
              result = 0
              # Only look for zeroes
              if looking_for == 1:
                  looking_for = not looking_for
                  continue
              # First iteration, take trailing zeroes
              # or trailing ones into account
              if index == 0:
                  if count != 0:
                      # Trailing zeroes
                      try:
                          result = seen[index + 1] + 1
                      except IndexError:
                          result = 1
              # Last iteration
              elif index == len(seen) - 1:
                  result = 1 + seen[index - 1]
              else:
                  # One zero
                  if count == 1:
                      result = seen[index + 1] + seen[index - 1] + 1
                  # Multiple zeroes
                  else:
                      result = max(seen[index + 1], seen[index - 1]) + 1
              if result > max_result:
                  max_result = result
              looking_for = not looking_for
          return max_result
  
      def _build_seen_list(self, num):
          seen = []
          looking_for = 0
          count = 0
          for _ in range(self.MAX_BITS):
              if num & 1 != looking_for:
                  seen.append(count)
                  looking_for = not looking_for
                  count = 0
              count += 1
              num >>= 1
          seen.append(count)
          return seen