C# 中的位移位混乱
我有一些像这样的旧代码:
private int ParseByte(byte theByte)
{
byte[] bytes = new byte[1];
bytes[0] = theByte;
BitArray bits = new BitArray(bytes);
if (bits[0])
return 1;
else
return 0;
}
它很长,我想我可以像这样修剪它:
private int ParseByte(byte theByte)
{
return theByte >> 7;
}
但是,我没有得到与第一个函数相同的值。 该字节包含 00000000 或 10000000。我在这里缺少什么? 我使用了不正确的运算符吗?
I have some old code like this:
private int ParseByte(byte theByte)
{
byte[] bytes = new byte[1];
bytes[0] = theByte;
BitArray bits = new BitArray(bytes);
if (bits[0])
return 1;
else
return 0;
}
It's long and I figured I could trim it down like this:
private int ParseByte(byte theByte)
{
return theByte >> 7;
}
But, I'm not getting the same values as the first function. The byte either contains 00000000 or 10000000. What am I missing here? Am I using an incorrect operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题是,在第一个函数中,bits[0] 返回最低有效位,但第二个函数返回最高有效位。 要修改第二个函数以获取最低有效位:
要修改第一个函数以返回最高有效位,您应该使用位[7],而不是位[0]。
The problem is that, in the first function, bits[0] returns the least significant bit, but the second function is returning the most significant bit. To modify the second function to get the least significant bit:
To modify the first function to return the most significant bit, you should use bits[7] -- not bits[0].
第一个片段的等效功能是:
在第二个片段中,您检查最重要的位,在第一个片段中检查最不重要的位。
The equivalent function to the first snipet is:
In the second snipet you were chechink the most significative bit and in the first snipet the least significant.
你想返回 int 还是 string? 无论如何 - 你可以使用模数:
好的,你编辑了......并且想要返回 int
一个单词到你的移位操作:你必须使用 << 而不是>>。 但这会返回(当您转换为 byte 而不是 int 时)0 或 128,而不是 0 或 1。因此,您可以将第二个解决方案重写为:
但其他答案包含比这更好的解决方案。
Do you want to return int or string? Anyway - you can use modulo:
OK, you edited ... and want to return int
A word to your shifting operation: you would have to use << instead of >>. But this returns (when you cast to byte instead of int) 0 or 128 and not 0 or 1. So you could rewrite your second solution as:
But the other answers contain really better solutions than this.
也许第一个函数应该检查bits[7]?
Perhaps the first function should check for bits[7] ?
二进制数中有一个额外的零(每个二进制数有 9 个数字)。 我假设这只是一个错字。
您确定您的订购正确吗? 二进制传统上是从右到左书写的,而不是像大多数其他计数系统那样从左到右书写。 如果您显示的二进制数字是属性格式的(意味着
10000000
实际上是数字128
而不是数字1
),那么您的第一个代码片段不应该工作,第二个应该工作。 如果您向后写入(意味着10000000
是1
,而不是128
),那么您甚至不需要位移。 只需将其与 1 进行 AND 运算 (theByte & 1
)。事实上,无论采用哪种方法,按位 AND(
&
运算符)似乎更合适。 鉴于您的第一个函数有效,而第二个函数无效,我假设您只是向后写了数字,并且需要将其与 1 进行 AND 运算,如上所述。You have an extra zero in your binary numbers (you have 9 digits in each). I'm assuming that's just a typo.
Are you sure you're doing your ordering correctly? Binary is traditionally written right-to-left, not left-to-right like most other numbering systems. If the binary number you showed is property formatted (meaning that
10000000
is really the number128
and not the number1
) then your first code snippet shouldn't work and the second should. If you're writing it backwards (meaning10000000
is1
, not128
), then you don't even need to bitshift. Just AND it with 1 (theByte & 1
).In fact, regardless of the approach a bitwise AND (the
&
operator) seems more appropriate. Given that your first function works and the second does not, I'm assuming you just wrote the number backwards and need to AND it with 1 as described above.据微软网站上的用户称BitArray 在内部将位按位顺序以大尾数法存储到 Int32 中。 这可能会导致问题。 如需解决方案和更多信息,您可以访问该链接。
According to a user on Microsoft's site the BitArray internally stores the bits into Int32s in big endian in bit order. That could cause the problem. For a solution and further info you can visit the link.
第一个函数不起作用,因为它尝试返回字符串而不是 int。
但您可能想要的是这样的:
但是您可能还想要这样:
1st The first function does not work as it tries to return a string instead of an int.
But what you might want is this:
However you might also want this: