C#位掩码验证
我对位掩码验证有一个小问题,如下所示:C#位掩码验证,c#,bitmask,C#,Bitmask,我对位掩码验证有一个小问题,如下所示: ... if (BitExist("52","0x20")) { //do something } ... ... Private bool BitExist(String value, String key) { //how can i make it return true? } 我的主要问题是,value&key是一个字符串值。有没有一个简单的方法可以让它工作?我对这个比特面具很陌生。如果有人能帮我,我真的很感激 private bool
...
if (BitExist("52","0x20"))
{
//do something
}
...
...
Private bool BitExist(String value, String key)
{
//how can i make it return true?
}
我的主要问题是,value&key是一个字符串值。有没有一个简单的方法可以让它工作?我对这个比特面具很陌生。如果有人能帮我,我真的很感激
private bool BitExists(string value, string key)
{
int k = Int32.Parse(key, System.Globalization.NumberStyles.AllowHexSpecifier);
return (Int32.Parse(value) & k) == k;
}
此代码段的作用如下。在括号内,键描述的一位被隔离
00110100
&00100000
---------
00100000
之后,您必须确定是否检查隔离位:
00100000 == 00100000 = true
将字符串解析为数字并使用&operator。为什么它们是字符串?除了Int32.Parse不理解
0x20
外,松开“0x”并使用int.Parse(key,System.Globalization.NumberStyles.allowehexspecifier)代码>太棒了!!!它起作用了!谢谢你的回答和额外的解释!感谢Henk Holterman提供了解决方案的密钥:)至于为什么它们是字符串,是因为我是从XML文件中读取的。