You can declare your own array type in C ++ and overload the operator&() function:
template<typename T, size_t N> class MyArray { template<typename X, size_t M> friend MyArray<X,M> operator&(const MyArray<X,M>& a, MyArray<X,M>& b) { MyArray<X,M> c; for(size_t i = 0; i < M; ++i) { c.array_[i] = a.array_[i] & b.array_[i]; } } std::array<T,N> array_; public: MyArray() { } MyArray(std::initializer_list l) array_(l) { }
And use it like
MyArray<int,100> a { 1,1, }; MyArray<int,100> b { 2,5, }; MyArray<int,100> c = a&b;
It is also possible to overload the operator&() function for std::array directly (perhaps the easiest way, as @Jarod suggested):
template<typename T, size_t N> std::array<T,N> operator&(const std::array<T,N>& a, std::array<T,N>& b) { std::array<T,N> c; std::transform(std::begin(a), std::end(a), std::begin(b), std::begin(c), [](T i1, T i2) { return i1 & i2; }); return c; }
source share