title: Swap Bits In A Given Number
signature: swap_bits_in_a_given_number <- function(x, p1, p2, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(x, p1, p2, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(x, p1, p2, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          95L, 88L, 97L, 92L, 
          95L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          16L, 26L, 59L, 42L, 
          16L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          55L, 56L, 40L, 41L, 
          55L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          75L, 35L, 79L, 30L, 
          75L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          90L, 12L, 59L, 34L, 
          90L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          58L, 65L, 25L, 19L, 
          58L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          69L, 64L, 17L, 94L, 
          69L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          36L, 33L, 97L, 44L, 
          36L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          62L, 69L, 66L, 9L, 
          62L
      )))
  
  }
  
  main()
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;
  }