title: Swap Bits In A Given Number
signature: func swapBitsInAGivenNumber(x int, p1 int, p2 int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(x int, p1 int, p2 int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(x, p1, p2, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver(95, 
  	88, 
  	97, 
  	92, 95)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver(16, 
  	26, 
  	59, 
  	42, 16)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver(55, 
  	56, 
  	40, 
  	41, 55)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver(75, 
  	35, 
  	79, 
  	30, 75)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver(90, 
  	12, 
  	59, 
  	34, 90)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver(58, 
  	65, 
  	25, 
  	19, 58)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver(69, 
  	64, 
  	17, 
  	94, 69)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver(36, 
  	33, 
  	97, 
  	44, 36)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver(62, 
  	69, 
  	66, 
  	9, 62)
      fmt.Printf("TEST-8...%s\n", 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;
  }