我对标志位有疑问。 我有一个
int
变量来保存标志。首先,我为该变量设置了一些标志。稍后我需要检查该变量中设置了多少个标志。但我不知道该怎么做。
检查是否设置了位值:
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)
}
需要注意的是,您不应将检查值设置为 0,除非它代表 All 或 None(并且不要使用按位逻辑进行比较;只需使用
value == 0
),因为任何 value & 0
始终为 0。
如果你想检查
a
是否设置了b
中的所有标志位,可以检查为:
(a & b) == b
我正在使用以下内容:
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
来代替,以增加类型安全性。
这是我在项目中使用的实用程序类
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
// ------------------------------------------------------------------------
}