简体   繁体   中英

Working of bitwise XOR on int operands of arrays

I am just a beginner.So, the problem that I am facing is :-

How does the logic of bitwise XOR works in case of arrays?

For example in the code below:-

// Function to return the only odd occurring element
int findOdd(int arr[], int n) {
   int res = 0, i;
   for (i = 0; i < n; i++)
     res ^= arr[i];
   return res;
}

int main(void) {
   int arr[] = {12, 12, 14, 90, 14, 14, 14};
   int n = sizeof(arr)/sizeof(arr[0]);
   printf ("The odd occurring element is %d ", findOdd(arr, n));
   return 0;
}

How, exactly, is the whole findOdd function working? Can someone please explain the basic logic behind the use of xor in the above code example?

You have used bitwise XOR , so it deals with binary bytes. It is not directly applied on array but on binary representation of data stored in it .

And what function does is to find number with odd occurrence in an array .

And using property of XOR operations -

XOR of any number with itself gives us 0 ,

XOR of any number with 0 gives us number itself.

We can understand the operations in each iteration-

1 iteration-  res=0(0000)^12(1100) -> res= 12(1100)  // just writing in decimal for clarity 
2 iteration-  res=12(1100)^12(1100) -> res=0(0000)   //operation is done on its binary representation
3 iteration-  res=0^14 ->  res=14
4 iteration-  res=14 (00001110) ^ 90 (01011010) ->  res=84 (01010100)
5 iteration-  res=84^14-> res=90
6 iteration-  res=90^14 -> res=84
7 iteration-  res=84^14-> res=90 

Therefore , res return 90 as result.

When do we use the bitwise XOR in arrays?

It perform operations on bits , not on a whole array.And is used in similar way as we use it on two numbers.

Working of xor on arrays is not different from normal xor or bitwise xor . To understand the logic better, following are the properties of xor .

  • xor of same bits returns 0
  • xor of different bits returns 1

Now, The array contains only one element that occurs odd number of times. So, When xor is applied on two same numbers, result is 0 and xor of 0 and any number gives the number itself.

Hence, when we xor all the elements in the array, even occurrences cancel out by returning 0 and final result remains as the only odd occurring element.

HTH.

The findOdd function uses the ^ or bitwise xor operator.

You already know about arithmetic operators like +, or -, etc.

The xor operator has some properties in common with addition and multiplication. It is:

  1. Commutative (ie. A xor B is the same as B xor A)
  2. Associative (ie. A xor (B xor C) is the same as (A xor B) xor C).

The bitwise xor simply does the xor operation on each bit of the operands in parallel. Given these two properties, we can xor the array elements in any order without altering the result.

Now, consider the bitwise xor of 2 numbers. It is always 0:

A^A == 0

For every evenly occurring number, we can pair them up, and the bitwise xor of these pairs are 0.

Therefore, the bitwise xor of the array, is the bitwise xor of the oddly occurring elements of the array.

If there is only one element which occurs an odd number of times, findOdd will return this number.

However, be warned, that if there are more than one set of oddly occurring numbers, the function will not do what you might expect.

Eg. taking the xor of {5, 2, 2, 7} , the 2's cancel out, but we are left with, which is 5^7 = 0b101 ^ 0b111 = 0b010 = 2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM