0

I'm trying to write a method that takes as input 2 integer arrays and compares them based on the binary & between each element and takes into consideration if one array is derived from the other. Also the size of the arrays is known and equal.

So basically if the method takes as input A = [2, 0, 0, 0] and B = [6, 0, 0, 3] it should return true because (2 & 6) != 0 (careful with the brackets here hehe) but if it takes the opposite A = [6, 0, 0, 3] and B = [2, 0, 0, 0] it should return false because A is derived from B (or B is contained in A).

An array is contained in another if by applying the & operator between two elements located at the same position you get true (basically the case where the method should return true).

So far I've got the following code which obviously doesn't work because it doesn't take into consideration if one array is derived from the other. All the operations I can think of using are commutative and it doesn't help.

private boolean checkContainment(int[] firstArr, int[] secondArr)
{
    List<Integer> posList = new ArrayList<Integer>();

    for(int i = 0;i < firstArr.length;i++)
    {
        if((firstArr[i] & secondArr[i]) != 0)
        {
            posList.add(i);
        }
    }

    for(int j = 0;j < firstArr.length;j++)
    {
        if(posList.size() > 0)
        {
            for(int k : posList)
            {
                if((j != k) && (firstArr[j] != secondArr[j]))
                {
                    return true;                        
                }
            }
        }
    }

    return false;
}

I'm pretty sure it's quite simple to tweak the following code in order to get it right but I can't get my head around it.

Hazerd
  • 463
  • 1
  • 6
  • 21
  • *So basically if the method takes as input A = [2, 0, 0, 0] and B = [6, 0, 0, 3] it should return true because (2 & 6) != 0 (careful with the brackets here hehe)*. Why should it return `true`? Why should it return `false`? – Elliott Frisch Aug 27 '14 at 13:03
  • 1
    could you define containment and derivation in this case? – user2504380 Aug 27 '14 at 13:13
  • well it should return true because (A[0] & B[0]) == TRUE , otherwise it should return FALSE when you have (B,A) as parameters because you can derive B from A since B is A with stuff added aka B is derived from A. – Hazerd Aug 27 '14 at 13:27
  • As far as I'm concerned, 2 & 6 is the same with 6 & 2, since 0010 & 0110 is 0010 in terms of binary AND operation. – DeiAndrei Aug 27 '14 at 13:57
  • 6 is made from 2, since 2 is 0010 and 6 is 0110. You add something to 2 to get 6, hence 6 is derived from 2 and [6,0,0,3] is derived from [2,0,0,0]. – Hazerd Aug 28 '14 at 09:04

1 Answers1

1

I think you are looking for implication or A implies B. That's the bitwise operation that makes your examples give the results you're looking for. You can't use the & to do that, as you and others have observed, since it doesn't give the results you want.

The way to compute A implies B using Java bitwise operators is ~A | B.

See this question for a truth table for logical implication. It's about C/C++ but the principles and the bitwise operators are pretty much the same as in Java.

Given int a[] and int b[] of the same length, here's a quick (Java 8) way to test bitwise implication of all values in the arrays:

IntStream.range(0, a.length)
    .map(i -> ~a[i] | b[i])
    .allMatch(n -> n == ~0)

Note that the allMatch predicate tests against ~0 since we want to test that all bits of all result values are ones.

Community
  • 1
  • 1
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259