C# 中的奇数位移位结果
假设我有一个 uint 值 2402914,并且我想获取最左边的 17 位,那么通过执行此代码,我的逻辑中的错误在哪里:
int testop = 0;
byte[] myArray = BitConverter.GetBytes(2402914);
fixed (byte* p = &myArray[0])
{
testop = *p >> 15;
}
我的预期输出是
50516.
Given that i have a uint value of 2402914, and i would like to grab the leftmost 17 bits, where is the fault in my logic by doing this code:
int testop = 0;
byte[] myArray = BitConverter.GetBytes(2402914);
fixed (byte* p = &myArray[0])
{
testop = *p >> 15;
}
my expected output is
50516.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想让您的期望符合现实。右移相当于除以 2。您实际上被 2 除十五次,这相当于除以 2^15 = 32768。请注意,2402914 / 32768 = 73(截断余数)。
因此,我预计结果是 73,而不是 50516。
事实上,
因此最左边的十七位是
注意 请
注意,您可以通过以下方式更简单地获得此结果
You might want to get your expectations to match reality. A right-shift is equivalent to dividing by 2. You are effectively dividing by 2 fifteen times, which is the same as saying you are dividing by 2^15 = 32768. Note that 2402914 / 32768 = 73 (truncating the remainder).
Therefore, I would expect the result to be 73, not 50516.
In fact,
So that the left-most seventeen bits are
Note that
Note that you can obtain this result more simply with
*p
只是给你第一个字节;它相当于p[0]
。您必须使用移位和 ORing 来组合前三个字节(或后三个字节,取决于字节顺序...)中的位。如果此代码不是更复杂的代码的简化版本,并且您实际上正在尝试只需从 int 中提取最左边的 17 位,这应该可以:(
编辑:添加
& 0x1ffff
以使其也适用于负整数;感谢@James。)*p
just gives you the first byte; it is equivalent top[0]
. You'll have to use shifting and ORing to combine bits from the first three bytes (or the last three bytes, depending on endianness...)If this code is not a simplified version of something more complicated and you're actually trying to just extract the leftmost 17 bits from an int, this should do:
(Edit: Added
& 0x1ffff
to make it work for negative integers too; thanks to @James.)哇,这是一个非常有趣的谜题。不是编程部分,而是试图找出你从哪里得到数字 50516 以及你想用你的代码做什么。看起来您正在取出 16 个最低有效位并将它们向左旋转 9 位。
我不知道你为什么使用字节数组,但似乎你认为你的fixed()语句正在循环遍历数组,但事实并非如此。固定块中的语句获取
myArray[0]
处的字节值并将其右移 15 位(移位用 0 填充,而不是旋转将前面的位绕到后面)。任何超过 8 的东西都会给你零。Wow, this has been a really fun puzzle to figure out. Not the programming part, but trying to figure out where you got the number 50516 and what you are trying to do with your code. It looks like you are taking the 16 least significant bits and ROTATING them LEFT 9 bits.
I don't know why you are using a byte array, but it seems like you think your fixed() statement is looping through the array, which it is not. Your statement in the fixed block is taking the byte value at
myArray[0]
and SHIFTing it right 15 bits (shifting fills with 0s as opposed to rotating which wraps the front bits around to the back). Any thing over 8 would give you zero.据我了解,您可以将位移运算符直接应用于 int 数据类型,而不必经历不安全代码的麻烦。
例如:
这与 Jason 预测的结果相关。
此外,我注意到
这表明您所需的结果无法通过任何类似的右移来实现。
From what I understand, you can apply the bit-shift operator directly to the int datatype, rather than going through the trouble of the unsafe code.
For example:
This ties to the result predicted by Jason.
Further, I note that
This suggests that your required result cannot be achieved by any similar right shift.