开发者

C# bitmask validation

开发者 https://www.devze.com 2023-04-10 18:25 出处:网络
I have a small problem with bitmask validation as below: ... if (BitExist(\"52\",\"0x20\")) { //do something

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

精彩评论

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