code: def reinverted(n, r):
      
      result = 0
      r = 1 << (r - 1)
      while n:
          if not n & 1:
              result |= r
          r >>= 1
          n >>= 1
      if r:
          result |= (r << 1) - 1
      return result
docs: Integer with reversed and inverted bits of n assuming bit length r.
  
      >>> reinverted(1, 6)
      31
  
      >>> [reinverted(x, 6) for x in [7, 11, 13, 14, 19, 21, 22, 25, 26, 28]]
      [7, 11, 19, 35, 13, 21, 37, 25, 41, 49]