title: What is the fastest way to XOR two integers in C#?
body: <p>I need to XOR one integer <code>a</code> against array of integers <code>q</code> (max of 100,000). i.e. if i am looping, I will </p>
  
  <p>a XOR q[0]</p>
  
  <p>a XOR q[1]</p>
  
  <p>..... </p>
  
  <p>a XOR q[100000] </p>
  
  <p>(100,000 times)</p>
  
  <p>I will have a series of such <code>a</code> to be XORed.</p>
  
  <p>I am writing a console application which will be pass the required input.</p>
  
  <p>I am using the built-in C# <code>^</code> operator to do the XOR operation. Is there any other way?</p>
  
  <p>Would converting the integer to a byte array and then XORing each bit and figuring out the end result be a good idea?</p>
  
  <p>Input (don't keep the spaces between the two lines)</p>
  
  <p>1</p>
  
  <p>15 8</p>
  
  <p>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15</p>
  
  <p>10 6 10</p>
  
  <p>1023 7 7</p>
  
  <p>33 5 8</p>
  
  <p>182 5 10</p>
  
  <p>181 1 13</p>
  
  <p>5 10 15</p>
  
  <p>99 8 9</p>
  
  <p>33 10 14</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace XOR
  {
      class Solution
      {
          static void Main(string[] args)
          {
  
              List&lt;TestCase&gt; testCases = ReadLine();
              //Console.WriteLine(DateTime.Now.ToLongTimeString());
              CalculationManager calculationManager = new CalculationManager();
  
              foreach (var testCase in testCases)
              {
                  var ints = testCase.Queries.AsParallel().Select(query =&gt; calculationManager.Calculate(query, testCase.SequenceOfIntegers)).ToList();
                  ints.ForEach(Console.WriteLine);
              }
  
              //Console.WriteLine(DateTime.Now.ToLongTimeString());
              //Console.ReadLine();
          }
  
          private static List&lt;TestCase&gt; ReadLine()
          {
              int noOfTestCases = Convert.ToInt32(Console.ReadLine());
              var testCases = new List&lt;TestCase&gt;();
  
              for (int i = 0; i &lt; noOfTestCases; i++)
              {
                  string firstLine = Console.ReadLine();
                  string[] firstLineSplit = firstLine.Split(' ');
                  int N = Convert.ToInt32(firstLineSplit[0]);
                  int Q = Convert.ToInt32(firstLineSplit[1]);
  
                  var testCase = new TestCase
                                     {
                                         Queries = new List&lt;Query&gt;(),
                                         SequenceOfIntegers = ReadLineAndGetSequenceOfIntegers()
                                     };
  
                  for (int j = 0; j &lt; Q; j++)
                  {
                      var buildQuery = ReadLineAndBuildQuery();
                      testCase.Queries.Add(buildQuery);
                  }
  
                  testCases.Add(testCase);
              }
  
              return testCases;
          }
  
          private static List&lt;int&gt; ReadLineAndGetSequenceOfIntegers()
          {
              string secondLine = Console.ReadLine();
              List&lt;int&gt; sequenceOfIntegers = secondLine.Split(' ').ToArray().Select(x =&gt; Convert.ToInt32(x)).ToList();
              return sequenceOfIntegers;
          }
  
          private static Query ReadLineAndBuildQuery()
          {
              var query = Console.ReadLine();
              List&lt;int&gt; queryIntegers = query.Split(' ').ToArray().Select(x =&gt; Convert.ToInt32(x)).ToList();
              Query buildQuery = ReadLineAndBuildQuery(queryIntegers[0], queryIntegers[1], queryIntegers[2]);
              return buildQuery;
          }
  
          private static Query ReadLineAndBuildQuery(int a, int p, int q)
          {
              return new Query { a = a, p = p, q = q };
          }
  
  
      }
  
      class CalculationManager
      {
          public int Calculate(Query query, List&lt;int&gt; sequenceOfIntegers)
          {
              var possibleIntegersToCalculate = FindPossibleIntegersToCalculate(sequenceOfIntegers, query.p, query.q);
              int maxXorValue = possibleIntegersToCalculate.AsParallel().Max(x =&gt; x ^ query.a);
              return maxXorValue;
          }
  
          private IEnumerable&lt;int&gt; FindPossibleIntegersToCalculate(List&lt;int&gt; sequenceOfIntegers, int p, int q)
          {
              return sequenceOfIntegers.GetRange(p - 1, (q - p) + 1).Distinct().ToArray();
          }
      }
  
      class TestCase
      {
          public List&lt;int&gt; SequenceOfIntegers { get; set; }
          public List&lt;Query&gt; Queries { get; set; }
      }
  
      class Query
      {
          public int a { get; set; }
          public int p { get; set; }
          public int q { get; set; }
      }
  }
  </code></pre>