Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top