# How to bitwise AND two arrays?

## Recommended Posts

Nietsnie    270
Hey, Is there a way to bitwise AND two different ararys together? Say I have:
char array1[100];
char array2[100];

Then the two arrays are filled with values somehow. If I try to do something like:
if (array1 & array2)
doSomething();

(basically checking for overlap in the arrays) the compile throws out an error. Any ideas? Thanks!

##### Share on other sites
Hodgman    51222
What you're doing there is trying to bitwise-and the *addresses* of the two arrays.
You probably want a loop instead, so you can check each element of the array.
for( int i=0; i<100; ++i ){//for each element in the array  if( array1[i] & array2[i] )//check for overlap at the i'th element    break;//stop the loop, we found an overlap}if( i < 100 )//if the loop exited before getting to 100  doSomething();

##### Share on other sites
DevFred    840
Quote:
 Original post by NietsnieIf I try to do something like:if (array1 & array2) doSomething();(basically checking for overlap in the arrays) the compile throws out an error.

What exactly do you mean by "checking for overlap"? Can you post an example where two arrays overlap and one where they don't?

##### Share on other sites
Cornstalks    7030
Quote:
 Original post by Hodgman*** Source Snippet Removed ***

Why not:

for(int i = 0; i < 100; ++i){    // for each element in the array    if(array1[i] & array2[i]) // check for overlap at the i'th element    {        doSomthing();        break; // stop the loop, we found an overlap    }}

You're last if statement was using i out of its scope.

Anyway, @ the OP, you'll want to be careful and make sure you don't overrun one or both of the arrays. But it's like Hodgman said: the arrays are just arrays of bytes, and if you want to bitwise AND them you have to do it manually, byte by byte.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by NietsnieIf I try to do something like:if (array1 & array2) doSomething();(basically checking for overlap in the arrays) the compile throws out an error.

What exactly do you mean by "checking for overlap"? Can you post an example where two arrays overlap and one where they don't?

The arrays overlapping would be when the character at the index of one array is equal to the character of the other array at the same index.

For example, array1[4] == array2[4];

What do you need to bitwise AND them for, anyway?

##### Share on other sites
Hodgman    51222
Quote:
 Original post by MikeTacularWhy not: *** Source Snippet Removed ***You're last if statement was using i out of its scope.
I put the DoSomething out of the loop so the logic of doing the overlap check was separate, but yeah, putting it in the loop is simpler. My bad about overlooking the scope tho!
Quote:
Original post by MortusMaximus
Quote:
 Original post by DevFredWhat exactly do you mean by "checking for overlap"?

What do you need to bitwise AND them for, anyway?
Nietsnie was ANDing to check for bit-wise overlap (not byte-wise overlap). I assume when expanding this to an entire array Nietsnie still wanted to check for bit-overlap, but who knows...

##### Share on other sites
DevFred    840
I would write a function template that works for any array element type and size:
template<typename T, size_t n>bool overlap(T const (&a)[n], T const (&b)[n]){    for (size_t i = 0; i < n; ++i)        if (a[i] & b[i]) return true;    return false;}

But I think you simply want to use std::bitset.