位操作-检查标志位j

我的标志位有问题。我有一个int变量来保存标志。 首先,我为该变量设置了一些标志。 稍后,我需要检查在该变量中设置了多少个标志。 但是我不知道这样做。

Nagaraju asked 2020-06-30T06:03:11Z
5个解决方案
80 votes

要检查是否设置了位值:

int value = VALUE_TO_CHECK | OTHER_VALUE_TO_CHECK;

if ((value & VALUE_TO_CHECK) == VALUE_TO_CHECK)
{
    // do something--it was set
}

if ((value & OTHER_VALUE_TO_CHECK) == OTHER_VALUE_TO_CHECK)
{
    // also set (if it gets in here, then it was defined in 
    //   value, but it does not guarantee that it was set with
    //   OR without other values. To guarantee it's only this
    //   value just use == without bitwise logic)
}

重要的是要注意,除非将其表示为All或None(并且不要使用按位逻辑进行比较;请使用value == 0),否则不应将其检查值为0,因为任何value & 0始终为0。

pickypg answered 2020-06-30T06:03:33Z
33 votes

另外,请考虑使用AbstractSet代替位字段。 另请参阅Bloch项目32。

附录:作为一个具体的例子:

枚举集还为传统的位标志提供了丰富的,类型安全的替代品:

EnumSet.of(Style.BOLD, Style.ITALIC);

请特别注意从AbstractSetAbstractCollection继承的便捷方法。

trashgod answered 2020-06-30T06:04:06Z
19 votes

如果要检查a是否在b中设置了所有标志位,则可以按以下方式进行检查:

(a & b) == b
Ates Goral answered 2020-06-30T06:04:26Z
10 votes

我正在使用以下内容:

public class BitFlags
{
    public static boolean isFlagSet(byte value, byte flags)
    {
        return (flags & value) == value;
    }

    public static byte setFlag(byte value, byte flags)
    {
        return (byte) (flags | value);
    }

    public static byte unsetFlag(byte value, byte flags)
    {
        return (byte) (flags & ~value);
    }
}

但是,如果您不需要“低级”,建议使用EnumSets代替,以增加类型安全性。

BullyWiiPlaza answered 2020-06-30T06:04:51Z
0 votes

这是我在项目中使用的Utility类

public class FlagUtils {

// ------------------------------------------------------------------------
// TYPES
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// STATIC FIELDS
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// STATIC METHODS
// ------------------------------------------------------------------------

/**
 * Sets the specified flags on the source int
 *
 * @param source the source int
 * @param flag   the flags which should be set
 *
 * @return the set int
 */
public static int setFlag(int source, int flag) {
    return source | flag;
}

/**
 * Un-sets the specified flags on the source int
 *
 * @param source the source int
 * @param flag   the flags which should be set
 *
 * @return the set int
 */
public static int unsetFlag(int source, int flag) {
    return source & ~flag;
}


/**
 * Check if the flags are set on the source ints
 *
 * @param source the source int
 * @param flag   the flags which should be set
 *
 * @return the set int
 */
public static boolean isFlagSet(int source, int flag) {
    return (source & flag) == flag;
}

/**
 * Flibs teh specified bit on the source
 *
 * @param source the source int
 * @param flag   the flags which should be set
 *
 * @return the set int
 */
public static int flip(int source, int flag) {
    return source & ~flag;
}

/**
 * Returns the masked int
 *
 * @param source the source int
 * @param mask
 *
 * @return the set int
 */
public static int mask(int source, int mask) {
    return source & mask;
}


// ------------------------------------------------------------------------
// FIELDS
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// CONSTRUCTORS
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// METHODS
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// GETTERS / SETTTERS
// ------------------------------------------------------------------------

}

Szabolcs Becze answered 2020-06-30T06:05:15Z
translate from https://stackoverflow.com:/questions/6067411/checking-flag-bits-java