c - Working of bitwise XOR on int operands of arrays -


i beginner.so, problem facing :-

how logic of bitwise xor works in case of arrays?

for example in code below:-

// function return odd occurring element int findodd(int arr[], int n) {    int res = 0, i;    (i = 0; < 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 %d ", findodd(arr, n));    return 0; } 

how, exactly, whole findodd function working? can please explain basic logic behind use of xor in above code example?

you have used bitwise xor , deals binary bytes. not directly applied on array on binary representation of data stored in .

and function to find number odd occurrence in array.

and using property of xor operations -

xor of number gives 0,

xor of number 0 gives number itself.

we can understand operations in each iteration-

1 iteration-  res=0(0000)^12(1100) -> res= 12(1100)  // writing in decimal clarity  2 iteration-  res=12(1100)^12(1100) -> res=0(0000)   //operation done on 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 result.

when use bitwise xor in arrays?

it perform operations on bits , not on whole array.and used in similar way use on 2 numbers.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -