本文介绍了检查标志位的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有标志位的问题。
我有一个int变量保存标志。首先,我设置一些标志变量。后来,我需要检查有多少个旗变量分别设置。但我不知道这样做。
请帮帮我。
谢谢,
Nagaraju。
I have a problem with flag bits.I have an int variable to hold flags. first i set some flags to that variable. later i need check how many flags were set in that variable. But i don't know to do it.please help me.Thank you,Nagaraju.
推荐答案
要检查,看是否有位值设置:
To check to see if a bit value is set:
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,除非它再presents全部或无(不要使用按位逻辑来比较是很重要的;只需使用价值== 0
),因为任何价值和放大器; 0
始终为0。
It's important to note that you should not have a checked value as 0 unless it represents All or None (and don't use bitwise logic to compare; just use value == 0
) because any value & 0
is ALWAYS 0.
这篇关于检查标志位的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!