Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use bitwise operations on arrays(a long piece of memory)

Tags:

There are two arrays as follows

int a[100]={1,1,...}
int b[100]={2,5,...}

Is there any way to operate them like this

c=a|b

the result of c is

{3,5,...}

I want to operate memory directly like memcpy

I don't want to use loop

like image 529
feng men Avatar asked Aug 24 '17 16:08

feng men


People also ask

Are bitwise operations fast?

On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition.

How does bitwise and operator work?

The bitwise AND operator ( & ) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise AND operator must have integral types.

What is bitwise and c++?

The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.


2 Answers

The Bitwise operations works by moving elements of the operation into registers and then doing the operation on registers. That means you are limited to the size of registers for your operations which is hardware dependent but at best is 64 bit (or 128 bit in some new hardware cases). This means even with some trickery, you would be able to do 2 bitwise operations at the same time. If speed is your concern, I would suggest using parallel for achieving your result. I also have to mention that your example creates the arrays on the stack witch limits the size of your array , which means you won't be able to see noticeable speed changes.

like image 133
Sam Avatar answered Sep 22 '22 11:09

Sam


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 that like

MyArray<int,100> a { 1,1, /* ... */ };
MyArray<int,100> b { 2,5, /* ... */ };

MyArray<int,100> c = a&b;

It's also possible to overload that operator&() function for std::array directly (probably easiest way as @Jarod proposed):

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;
}
like image 28
user0042 Avatar answered Sep 22 '22 11:09

user0042