title: Swap Bits In A Given Number
signature: class Solution {
  
      public int SwapBitsInAGivenNumber(int x, int p1, int p2, int n) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(int x, int p1, int p2, int n, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(x, p1, p2, n); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  95, 88, 97, 92, 
                  95
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  16, 26, 59, 42, 
                  16
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  55, 56, 40, 41, 
                  55
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  75, 35, 79, 30, 
                  75
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  90, 12, 59, 34, 
                  90
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  58, 65, 25, 19, 
                  58
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  69, 64, 17, 94, 
                  69
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  36, 33, 97, 44, 
                  36
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  62, 69, 66, 9, 
                  62
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );
          }
      }
  }
  
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;
  }