title: Swap Bits In A Given Number
signature: function swap_bits_in_a_given_number(x, p1, p2, n)
source_py: def swap_bits_in_a_given_number(x, p1, p2, n):
      set1 = ((x >> p1) & ((1 << n) - 1))
      set2 = ((x >> p2) & ((1 << n) - 1))
      xor = (set1 ^ set2)
      xor = ((xor << p1) | (xor << p2))
      result = (x ^ xor)
      return result
source_cpp: int swap_bits_in_a_given_number(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n) {
    unsigned int set1 = ( x >> p1 ) & ( ( 1U << n ) - 1 );
    unsigned int set2 = ( x >> p2 ) & ( ( 1U << n ) - 1 );
    unsigned int Xor = ( set1 ^ set2 );
    Xor = ( Xor << p1 ) | ( Xor << p2 );
    unsigned int result = x ^ Xor;
    return result;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(x, p1, p2, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(x, p1, p2, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver(95, 88, 97, 92, 95)
      print(string.format("TEST-0...%s",result))
  
      result = driver(16, 26, 59, 42, 16)
      print(string.format("TEST-1...%s",result))
  
      result = driver(55, 56, 40, 41, 55)
      print(string.format("TEST-2...%s",result))
  
      result = driver(75, 35, 79, 30, 75)
      print(string.format("TEST-3...%s",result))
  
      result = driver(90, 12, 59, 34, 90)
      print(string.format("TEST-4...%s",result))
  
      result = driver(58, 65, 25, 19, 58)
      print(string.format("TEST-5...%s",result))
  
      result = driver(69, 64, 17, 94, 69)
      print(string.format("TEST-6...%s",result))
  
      result = driver(36, 33, 97, 44, 36)
      print(string.format("TEST-7...%s",result))
  
      result = driver(62, 69, 66, 9, 62)
      print(string.format("TEST-8...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 95, "inputs": {"x": 95, "p1": 88, "p2": 97, "n": 92}}, {"idx": 1, "outputs": 16, "inputs": {"x": 16, "p1": 26, "p2": 59, "n": 42}}, {"idx": 2, "outputs": 55, "inputs": {"x": 55, "p1": 56, "p2": 40, "n": 41}}, {"idx": 3, "outputs": 75, "inputs": {"x": 75, "p1": 35, "p2": 79, "n": 30}}, {"idx": 4, "outputs": 90, "inputs": {"x": 90, "p1": 12, "p2": 59, "n": 34}}, {"idx": 5, "outputs": 58, "inputs": {"x": 58, "p1": 65, "p2": 25, "n": 19}}, {"idx": 6, "outputs": 69, "inputs": {"x": 69, "p1": 64, "p2": 17, "n": 94}}, {"idx": 7, "outputs": 36, "inputs": {"x": 36, "p1": 33, "p2": 97, "n": 44}}, {"idx": 8, "outputs": 62, "inputs": {"x": 62, "p1": 69, "p2": 66, "n": 9}}]
question_info.extension: lua
question_info.entry_fn_name: swap_bits_in_a_given_number
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8
question_info.commands: lua __FILENAME__