bit-manipulation

XOR of pairwise sum of every unordered pairs in an array

狂风中的少年 提交于 2020-12-04 05:13:48
问题 Question Description : Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. I solved this question using the method described in this post. My Code : int xorAllSum(int a[], int n) { int curr, prev = 0; int ans = 0; for (int k = 0; k < 32; k++) { int o = 0, z = 0; for (int i = 0; i < n; i++) { if (a[i] & (1 << k)) { o++; } else { z++; } } curr = o * z + prev; if (curr & 1) { ans = ans | (1 << k); } prev = o * (o - 1) / 2;

XOR of pairwise sum of every unordered pairs in an array

走远了吗. 提交于 2020-12-04 05:09:44
问题 Question Description : Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. I solved this question using the method described in this post. My Code : int xorAllSum(int a[], int n) { int curr, prev = 0; int ans = 0; for (int k = 0; k < 32; k++) { int o = 0, z = 0; for (int i = 0; i < n; i++) { if (a[i] & (1 << k)) { o++; } else { z++; } } curr = o * z + prev; if (curr & 1) { ans = ans | (1 << k); } prev = o * (o - 1) / 2;

XOR of pairwise sum of every unordered pairs in an array

一世执手 提交于 2020-12-04 05:09:13
问题 Question Description : Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. I solved this question using the method described in this post. My Code : int xorAllSum(int a[], int n) { int curr, prev = 0; int ans = 0; for (int k = 0; k < 32; k++) { int o = 0, z = 0; for (int i = 0; i < n; i++) { if (a[i] & (1 << k)) { o++; } else { z++; } } curr = o * z + prev; if (curr & 1) { ans = ans | (1 << k); } prev = o * (o - 1) / 2;