Java:用掩码检查二进制值然后获取整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12473509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:02:37  来源:igfitidea点击:

Java: Check binary value with a mask then get integer

javabit-manipulationbitmask

提问by n0n0bstan

Here is my problem:

这是我的问题:

I have a binary value

我有一个二进制值

101001

and a mask

和一个面具

011100

I would like to compare them and get the result as an integer. In this case that would give:

我想比较它们并将结果作为整数。在这种情况下,这将给出:

 1 **010** 01
 0 **111** 00

= 010 => 2

My first idea consists of dealing with a character array. But I would like to know if there is a better way to achieve this aim in Java?

我的第一个想法是处理字符数组。但我想知道在 Java 中是否有更好的方法来实现这一目标?

采纳答案by n0n0bstan

I just improved the algorithm to be able to have mask where one bits can be splitted such as:

我只是改进了算法,以便能够拥有可以拆分一位的掩码,例如:

00111011011

Here is my function to get a value from a mask and a masked value

这是我从掩码和掩码值中获取值的函数

 private static long getMaskedValue(long maskedValue, long mask){
        long definitiveMaskedValue = 0;
        int count=0;

        maskedValue = mask & maskedValue;

        while (mask != 0){
            while ((mask & 1) == 0){
                mask = mask >>> 1;
                maskedValue = maskedValue >>> 1;
            }
            while ((mask & 1) == 1){
                definitiveMaskedValue = definitiveMaskedValue + ((maskedValue & 1) << count);
                count++;

                mask = mask >>> 1;
                maskedValue = maskedValue >>> 1;
            }
        }

        return definitiveMaskedValue;
    }

Here is my function to store a value in a variable thanks to a bitmask, it returns the old variable with the value stored inside. I had to use BigInteger because of the shift operator that can not shift more than 32bits left in Java.

由于位掩码,这是我将值存储在变量中的函数,它返回旧变量和存储在其中的值。我不得不使用 BigInteger,因为移位运算符在 Java 中不能移位超过 32 位。

private static long setMaskedValue (long maskedValue, long mask, long valueToAdd) {
            int nbZero=0;
            int nbLeastSignificantBit=0;
            long tmpMask=mask;
            maskedValue = maskedValue & ~mask;

            while (tmpMask != 0){
                while ((tmpMask & 1) == 0){
                    tmpMask = tmpMask >>> 1;
                    nbLeastSignificantBit++;
                    nbZero ++;
                }

                while ((tmpMask & 1) == 1){
                    tmpMask = tmpMask >>> 1;

                    BigInteger bigValueToAdd = BigInteger.valueOf(valueToAdd).shiftLeft(nbZero);
                    long tmpValueToAdd = bigValueToAdd.longValue();
                    BigInteger bigMaskOneBit = BigInteger.valueOf(1).shiftLeft(nbLeastSignificantBit);
                    long maskOneBit = bigMaskOneBit.longValue();

                    long bitValueToSet = getMaskedValue(tmpValueToAdd, maskOneBit);
                    maskedValue = maskedValue | bitValueToSet << nbLeastSignificantBit;
                    nbLeastSignificantBit++;
                }
            }
        return maskedValue;
    }

回答by user207421

I would like to compare them and get the result as an integer

我想比较它们并将结果作为整数

Assuming you meant 'mask' rather than 'compare':

假设您的意思是“掩码”而不是“比较”:

int result = 0B011100 & 0B011100;

No char arrays required.

不需要字符数组。

This is rather trivial.

这是相当琐碎的。

回答by helios

Of course.

当然。

  1. You need first AND your bits.
  2. Shift right to avoid those zeros at right of mask.
  1. 你首先需要和你的位。
  2. 右移以避免掩码右侧的那些零。

You need value as integer already.

您已经需要作为整数的值。

Then do the AND: int masked = value & mask;

然后做AND: int masked = value & mask;

Then shift right until the first 1 in the mask.

然后右移直到掩码中的第一个 1。

while (mask % 2 == 0) {
   mask = mask >>> 1;
   masked = masked >>> 1;
}

You can use while (mask & 1 == 0) {if you prefer :)

while (mask & 1 == 0) {如果您愿意,可以使用:)



& is bitwise AND.
| is bitwise OR.
^ is bitwise XOR (if my memory doesn't fail :).
>>> is shifting right (unsigned integer)