How to bitwise AND two arrays?

Started by
5 comments, last by DevFred 14 years, 11 months ago
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!
Advertisement
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 & array2 )//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();
Quote:Original post by Nietsnie
If 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?
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 & array2) // 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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by DevFred
Quote:Original post by Nietsnie
If 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?
Check out the first gameplay video from my javascript/PHP RTS game
Quote:Original post by MikeTacular
Why 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 DevFred
What 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...
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 & b) return true;    return false;}

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

This topic is closed to new replies.

Advertisement