开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜