C# bitmask validation
I have a small problem with bitmask validation as below:
...
if (BitExist("52","0x20"))
{
//do something
}
...
...
Private bool BitExist(String value, String key)
{
//how can i make it return true?
}
开发者_如何转开发
My main problem is that, the value & key is a string value. Is there an easy way to make this works? I'm very new to this bitmask thingy. Really appreciate it if someone can help me out.
private bool BitExists(string value, string key)
{
int k = Int32.Parse(key, System.Globalization.NumberStyles.AllowHexSpecifier);
return (Int32.Parse(value) & k) == k;
}
What this code snippet does is the following. Inside the bracket the one bit described by key is isolated.
00110100
&00100000
---------
00100000
After that you have you have to determine whether or not the isolated bit is checked:
00100000 == 00100000 = true
精彩评论