Home > Java > javaTutorial > In Java, find array using different XOR operations on elements using groups of size 4

In Java, find array using different XOR operations on elements using groups of size 4

WBOY
Release: 2023-08-26 10:45:07
forward
1274 people have browsed it

In Java, find array using different XOR operations on elements using groups of size 4

Translate the following sentence into Chinese, retain the html code, and do not need to add new content:

We are given an integer array of size N (size that is a multiple of 4) and we must Performs an XOR operation on the array so that input[1- 4] looks like utility_arr[1- 4] and the calculation condition are if arr[1 – 4] = {a1, a2, a3, a4} Then q[1 – 4] = {a1 ⊕ a2 ⊕ a3, a1 ⊕ a2 ⊕ a4, a1 ⊕ a3 ⊕ a4, a2 ⊕ a3 ⊕ a4}

Let’s look at the various inputs for this situation Output scenario-

In − int[] input = { 5, 2, 3, 4 };

Out − after XOR Result Operation 4 3 2 5

Explanation−The output of an XOR gate only goes "high" when its two input terminals are at "different" logic levels. If both inputs A and B are at logic level "1" or "0", the output is "0", making the gate an "odd gate but not an even gate". In other words, when the input has an odd number of 1's, the output is "1".

a1 ⊕ a2 ⊕ a3 = 5 ⊕ 2 ⊕ 3 = 4

a1 ⊕ a2 ⊕ a4 = 5 ⊕ 2 ⊕ 4 = 3

a1 ⊕ a3 ⊕ a4 = 5⊕ 3 ⊕ 4 = 2

a2 ⊕ a3 ⊕ a4 = 2 ⊕ 3 ⊕ 4 = 5

In − int[] input = { 7, 6, 4, 4, 3, 8, 9, 5 };

Out − Result after XOR operation 5 5 7 6 2 14 15 4

Explanation − The output of an XOR gate only goes "high" when its two input terminals are at "different" logic levels from each other. If both inputs A and B are at logic level "1" or "0", the output is "0", making the gate an "odd gate but not an even gate". In other words, when the input has an odd number of 1's, the output is "1". Only works for input[] sizes that are a multiple of 4, input arrays of other sizes will display 0s in place of numbers in odd positions.

The result after XOR operation 5 5 7 6 2 14 15 4

The method used in the following program is as follows-

  • According to XOR a ⊕ Properties of a = 0 and a ⊕ 0 = a. (a ⊕ b ⊕ c) ⊕ (b ⊕ c ⊕ d) = a ⊕ d (As (b ​​⊕ c) ⊕ (b ⊕ c) = 0)

  • For calculation Divide the array into 4 groups, and we will calculate the result of each group according to the properties of XOR.

  • Referring to the above properties, using (a ⊕ d) we can calculate b and c (a ⊕ b ⊕ d) ⊕ (a ⊕ d) = b (a ⊕ c ⊕ d) ⊕ (a ⊕ d) = c

  • By using b and c, we can get a and d using (a ⊕ b ⊕ c) ⊕ (b) ⊕ (c) = a (b ⊕ c ⊕ d) ⊕ (b) ⊕ (c) = d

  • Repeat the process for all four groups

  • Use 2 The pointers i and j iterate through the loop until the length of the array is divided by four, and introduce temporary values ​​(ans) and utility arrays (to store answers).

  • Implement the following XOR operation in the for loop

    ans= input array[i] ⊕ input array[i 3]

    Utility array [i 1] (calculate b) = input array [i 1] ⊕ ans

    practical array [i 2] (calculate c) = input array [i 2] ⊕ ans

    practical array [i](calculate a) = input array[i]⊕((utility array[i 1])^(utility array[i 2]))

    utility array[i](calculate d))= input array[i 3] ⊕ ((Utility array[i 1]) ^ (Utility array[i 2]))

  • and the pointer is updated for the next set of four characters

  • Finally, print the array and return the result to the user.

Example

import java.util.Arrays;
import java.util.List;
public class Tutorials{
   static int ans = 0;
   public static void main(String args[]){
      int[] input = {7, 1, 2, 3};
      int[] arr = new int[input.length];
      for (int i = 0, j = 0; j < input.length / 4; j++){
         ans = input[i] ^ input[i + 3];
         arr[i + 1] = input[i + 1] ^ ans;
         arr[i + 2] = input[i + 2] ^ ans;
         arr[i] = input[i] ^ ((arr[i + 1]) ^ (arr[i + 2]));
         arr[i + 3] = input[i + 3] ^ (arr[i + 1] ^ arr[i + 2]);
         i += 4;
      }
      System.out.println("Different XORs of elements in groups of size 4 is: ");
      for (int i = 0; i < arr.length; i++){
         System.out.println(arr[i]);
      }
   }
}
Copy after login

Output

If we run the above code, the following output will be generated

Different XORs of elements in groups of size 4 is :
4
5
6
0
Copy after login

The above is the detailed content of In Java, find array using different XOR operations on elements using groups of size 4. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template