简体   繁体   English

Java:使用二进制运算符比较两个数组

[英]Java: Comparison between two arrays with binary operator

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. 我正在尝试编写一种方法,将2个整数数组作为输入,并根据每个元素之间的二进制&比较它们,并考​​虑一个数组是否从另一个数组派生。 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). 因此,基本上,如果该方法将输入A = [2,0,0,0]且B = [6,0,0,3]作为输入,则应该返回true,因为(2&6)!= 0(请注意此处的括号) hehe), 如果取相反的A = [6,0,0,3]并且B = [2,0,0,0],则应该返回false,因为A是从B派生的(或者B包含在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). 如果通过将&运算符应用于位于相同位置的两个元素之间,则数组包含在另一个数组中,则该数组为true(基本上是该方法应返回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. 我敢肯定,调整以下代码以使其正确无误非常简单,但我无法理解。

I think you are looking for implication or A implies B . 我认为您正在寻找含义A暗示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 使用Java按位运算符来计算A暗含B的方法是~A | B ~A | B . ~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. 它是关于C / C ++的,但是其原理和按位运算符与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: 给定int a[]int b[]的长度相同,这是一种快速(Java 8)方法来测试数组中所有值的按位含义:

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. 需要注意的是allMatch对谓词测试~0 ,因为我们要测试的所有结果值的所有位为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM