Suppose I have two vectors std::vector<uint_32> a, b;
that I know to be of the same size.
Is there a C++11 paradigm for doing a bitwise-AND
between all members of a
and b
, and putting the result in std::vector<uint_32> c;
?
Suppose I have two vectors std::vector<uint_32> a, b;
that I know to be of the same size.
Is there a C++11 paradigm for doing a bitwise-AND
between all members of a
and b
, and putting the result in std::vector<uint_32> c;
?
A lambda should do the trick:
#include <algorithm>
#include <iterator>
std::transform(a.begin(), a.end(), // first
b.begin(), // second
std::back_inserter(c), // output
[](uint32_t n, uint32_t m) { return n & m; } );
Even better, thanks to @Pavel and entirely C++98:
#include <functional>
std::transform(a.begin(), a.end(), b.begin(),
std::back_inserter(c), std::bit_and<uint32_t>());
If you're going to be doing this a lot, on large arrays, check out the linear algebra libraries mentioned in https://stackoverflow.com/search?q=valarray. Many of them will take advantage of special instructions to get the answer faster.
Just an idea, not C++11 specific: Maybe you could step through the arrays 8 bytes at a time using uint_64, even though the actual array is composed of 32-bit integers? Then you would not rely on e.g. SSE, but still get fast execution on many CPUs that have 64-bit wide registers.