fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. std::size_t xor_sum(const std::vector<std::uint32_t>& v)
  6. {
  7. std::size_t res = 0;
  8.  
  9. for (std::size_t b = 0; b != 32; ++b) {
  10. const std::size_t count_0 =
  11. std::count_if(v.begin(), v.end(),
  12. [b](std::uint32_t n) { return (n >> b) & 0x01; });
  13. const std::size_t count_1 = v.size() - count_0;
  14. res += count_0 * count_1 << b;
  15. }
  16. return res;
  17. }
  18.  
  19. int main() {
  20. std::cout << xor_sum({3, 5, 7}); // 12 = 3^5 + 3^7 + 5^7
  21. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
12