📜  计算从1到n的XOR。

📅  最后修改于: 2021-04-23 07:32:21             🧑  作者: Mango

给定数字n,任务是找到从1到n的XOR。
例子 :

Input : n = 6
Output : 7
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6  = 7

Input : n = 7
Output : 0
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 0

方法1(天真的方法):
1-将结果初始化为0。
1-遍历从1到n的所有数字。
2-对结果进行一对一的异或运算。
3-最后,返回结果。
方法2(有效方法):
1-通过对n的余数乘以4求出余数。
2-如果rem = 0,则xor将与n相同。
3-如果rem = 1,则xor将为1。
4-如果rem = 2,则xor将为n + 1。
5-如果rem = 3,则xor将为0。

C++
// C++ program to find XOR of numbers
// from 1 to n.
#include
using namespace std;
 
// Method to calculate xor
int computeXOR(int n)
{
   
  // If n is a multiple of 4
  if (n % 4 == 0)
    return n;
 
  // If n%4 gives remainder 1
  if (n % 4 == 1)
    return 1;
 
  // If n%4 gives remainder 2
  if (n % 4 == 2)
    return n + 1;
 
  // If n%4 gives remainder 3
  return 0;
}
 
// Driver method
int main()
{
  int n = 5;
  cout<


Java
// Java program to find XOR of numbers
// from 1 to n.
 
class GFG
{
    // Method to calculate xor
    static int computeXOR(int n)
    {
        // If n is a multiple of 4
        if (n % 4 == 0)
            return n;
      
        // If n%4 gives remainder 1
        if (n % 4 == 1)
            return 1;
      
        // If n%4 gives remainder 2
        if (n % 4 == 2)
            return n + 1;
      
        // If n%4 gives remainder 3
        return 0;
    }
     
    // Driver method
    public static void main (String[] args)
    {
         int n = 5;
         System.out.println(computeXOR(n));
    }
}


Python 3
# Python 3 Program to find
# XOR of numbers from 1 to n.
 
# Function to calculate xor
def computeXOR(n) :
 
    # Modulus operator are expensive
    # on most of the computers. n & 3
    # will be equivalent to n % 4.
 
    # if n is multiple of 4
    if n % 4 == 0 :
        return n
 
    # If n % 4 gives remainder 1
    if n % 4 == 1 :
        return 1
 
    # If n%4 gives remainder 2
    if n % 4 == 2 :
        return n + 1
 
    # If n%4 gives remainder 3
    return 0
 
# Driver Code
if __name__ == "__main__" :
 
    n = 5
 
    # function calling
    print(computeXOR(n))
         
# This code is contributed by ANKITRAI1


C#
// C# program to find XOR
// of numbers from 1 to n.
using System;
 
class GFG
{
     
    // Method to calculate xor
    static int computeXOR(int n)
    {
        // If n is a multiple of 4
        if (n % 4 == 0)
            return n;
     
        // If n%4 gives remainder 1
        if (n % 4 == 1)
            return 1;
     
        // If n%4 gives remainder 2
        if (n % 4 == 2)
            return n + 1;
     
        // If n%4 gives remainder 3
        return 0;
    }
     
    // Driver Code
    static public void Main ()
    {
        int n = 5;
        Console.WriteLine(computeXOR(n));
    }
}
 
// This code is contributed by ajit


PHP


Javascript


输出 :

1

这是如何运作的?
当我们对数字进行XOR运算时,在4的倍数之前得到0作为XOR值。这在4的倍数之前不断重复。

Number Binary-Repr  XOR-from-1-to-n
1         1           [0001]
2        10           [0011]
3        11           [0000]  <----- We get a 0
4       100           [0100]  <----- Equals to n
5       101           [0001]
6       110           [0111]
7       111           [0000]  <----- We get 0
8      1000           [1000]  <----- Equals to n
9      1001           [0001]
10     1010           [1011]
11     1011           [0000] <------ We get 0
12     1100           [1100] <------ Equals to n