title: Swap Bits In A Given Number
signature: int swapBitsInAGivenNumber(int x, int p1, int p2, int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(int x, int p1, int p2, int n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(x, p1, p2, n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(95, 88, 97, 92, 95); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(16, 26, 59, 42, 16); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(55, 56, 40, 41, 55); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(75, 35, 79, 30, 75); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(90, 12, 59, 34, 90); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver(58, 65, 25, 19, 58); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver(69, 64, 17, 94, 69); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver(36, 33, 97, 44, 36); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver(62, 69, 66, 9, 62); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }