C#中的按位操作 [英] Bit wise operations in C#
问题描述
大家好,
看来我之前做的二进制混音是对的(但错了)但我需要
将二进制数作为位字段,下面的代码粗略地说明了我希望二进制数字中的4位数值
Hi all,
It appears that the binary mucking I did earlier was right (but wrong) it but I
need to go over the binary number as bit field the below code is a rough lash up of how I want the 4 bit values from the Binary number
char[] Input;
for (int i = 0; i < 4; i++)
{
if (Input[i] > 'Z')
Input[i] -= 0x20; // Convert lower case to upper case
if ((Input[i] >= '0') && (Input[i] <= '9')) // First check to see if the number is 0 to 9
{
Input[i] -= '0'; //If so, then subtract ascii ‘0’ from it
}
else if ((Input[i] >= 'A') && (Input[i] <= 'F'))// is this the hex codes A to F?
{
Input[i] -= 0x37; // Subtract 55 (0x37) so ascii A becomes 10
}
else
Input[i] = 0; // Else invalid so substitute 0
}
如果您和&使用0x08,结果不为0,然后设置第3(4)位
如果你&使用0x04并且结果不为0然后设置位2(3)
如果你&使用0x02并且结果不为0然后设置位1(2)
如果你&使用0x01并且结果不为0然后设置位0(1)
我想做旧的C路线(a&& amp; 2)等男人我得回家了!
格伦
(如果这个问题达到顶峰,我会改写并明天更清洁)
它有,所以改为时间我有一个0到15之间的字符,上面的代码(有点)将它转换为尝试获得0到15的价值。我相信应该有一个ConvertTo来做这个但是....
格伦
If you & with 0x08 and the result is not 0 then bit 3(4) is set
If you & with 0x04 and the result is not 0 then bit 2(3) is set
If you & with 0x02 and the result is not 0 then bit 1(2) is set
If you & with 0x01 and the result is not 0 then bit 0(1) is set
I want to do the old C route of doing it bit wise (a&&2) etc. Man I have to go home!
Glenn
(If the question peaks interest I will rephrase and make it cleaner tomorrow)
Umm it has, so rephrase time I have a character between 0 to 15 the code above (sort of) converted it to try to get 0 to 15 value out. I believe there should be a ConvertTo to do this but....
Glenn
推荐答案
在这段代码中,没有类似的东西位操作本身。请参阅运营商列表:
http: //msdn.microsoft.com/en-us/library/6a71f45d%28v=vs.110%29.aspx [ ^ ]。
你会的需要二进制移位''>>'',''<<'',二进制AND,OR,XOR和NOT(''&'',''|'','''''',' '〜'')。另请参阅:
http ://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html [ ^ ]。
-SA
In this code, there is nothing with resembles bit operations per se. Refer to the operator list:
http://msdn.microsoft.com/en-us/library/6a71f45d%28v=vs.110%29.aspx[^].
You will need binary shift ''>>'', ''<<'', binary AND, OR, XOR and NOT (''&'', ''|'', ''^'', ''~''). See also:
http://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html[^].
—SA
你可以参考检查位标志你可以在C#中执行此操作:
On the off hand chance you are refering to checking bit flags you could do this in C#:
public enum BitFlag : byte
{
First=1,
Second=2,
Third=4,
Fourth=8
}
public static string CheckFlag()
{
BitFlag bFlag = BitFlag.First | BitFlag.Third;
StringBuilder result = new StringBuilder();
if ((bFlag & BitFlag.First) == BitFlag.First)
result.AppendLine("First");
if ((bFlag & BitFlag.Second) == BitFlag.Second)
result.AppendLine("Second");
if ((bFlag & BitFlag.Third) == BitFlag.Third)
result.AppendLine("Third");
if ((bFlag & BitFlag.Fourth) == BitFlag.Fourth)
result.AppendLine("Fourth");
return result.ToString();
}
你做的是像
What you do is something like
int i = int.TryParse(s, System.Globalization.NumberStyles.AllowHexSpecifier, null, out i)
? i
: 0;
这与位运算符无关。
Andi
This has nothing to do with bit operators.
Andi
这篇关于C#中的按位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!