C# 中的奇数位移位结果

发布于 2024-11-25 01:11:53 字数 289 浏览 4 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

财迷小姐 2024-12-02 01:11:53

您可能想让您的期望符合现实。右移相当于除以 2。您实际上被 2 除十五次,这相当于除以 2^15 = 32768。请注意,2402914 / 32768 = 73(截断余数)。

因此,我预计结果是 73,而不是 50516。

事实上,

2402914_10 = 0000 0000 0010 0100 1010 1010 0110 0010_2

因此最左边的十七位是

             0000 0000 0010 0100 1

注意 请

0000 0000 0010 0100 1 = 1 * 1 + 0 * 2 + 0 * 4 + 1 * 8 + 0 * 16 + 0 * 32 + 1 * 64 
                      = 73

注意,您可以通过以下方式更简单地获得此结果

int testop = 2402914 >> 15;

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,

2402914_10 = 0000 0000 0010 0100 1010 1010 0110 0010_2

So that the left-most seventeen bits are

             0000 0000 0010 0100 1

Note that

0000 0000 0010 0100 1 = 1 * 1 + 0 * 2 + 0 * 4 + 1 * 8 + 0 * 16 + 0 * 32 + 1 * 64 
                      = 73

Note that you can obtain this result more simply with

int testop = 2402914 >> 15;
×纯※雪 2024-12-02 01:11:53

*p 只是给你第一个字节;它相当于p[0]。您必须使用移位和 ORing 来组合前三个字节(或后三个字节,取决于字节顺序...)中的位。

如果此代码不是更复杂的代码的简化版本,并且您实际上正在尝试只需从 int 中提取最左边的 17 位,这应该可以:(

int testop = (someInt >> 15) & 0x1ffff;

编辑:添加 & 0x1ffff 以使其也适用于负整数;感谢@James。)

*p just gives you the first byte; it is equivalent to p[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:

int testop = (someInt >> 15) & 0x1ffff;

(Edit: Added & 0x1ffff to make it work for negative integers too; thanks to @James.)

老街孤人 2024-12-02 01:11:53

哇,这是一个非常有趣的谜题。不是编程部分,而是试图找出你从哪里得到数字 50516 以及你想用你的代码做什么。看起来您正在取出 16 个最低有效位并将它们向左旋转 9 位。

2402914: 0000 0000 0010 0100 1010 1010 0110 0010
 left 9: 0100 1001 0101 0100 1100 010 
  match:                     ^^^^ ^^^  
>>50516:                     1100 0101 0101 0100
  match:                             ^ ^^^^ ^^^^
right 7:                             1 0101 0100 110 0010

int value2 = value & 0xffff;
int rotate9left = ((value2 << 9) & 0xffff) | ((value2) >> (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.

2402914: 0000 0000 0010 0100 1010 1010 0110 0010
 left 9: 0100 1001 0101 0100 1100 010 
  match:                     ^^^^ ^^^  
>>50516:                     1100 0101 0101 0100
  match:                             ^ ^^^^ ^^^^
right 7:                             1 0101 0100 110 0010

int value2 = value & 0xffff;
int rotate9left = ((value2 << 9) & 0xffff) | ((value2) >> (16 - 9));

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.

皇甫轩 2024-12-02 01:11:53

据我了解,您可以将位移运算符直接应用于 int 数据类型,而不必经历不安全代码的麻烦。

例如:

2402914 >> 15 = 73

这与 Jason 预测的结果相关。

此外,我注意到

2402914 >> 5 = 75091
and 2402914 >> 6 = 37545

这表明您所需的结果无法通过任何类似的右移来实现。

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:

2402914 >> 15 = 73

This ties to the result predicted by Jason.

Further, I note that

2402914 >> 5 = 75091
and 2402914 >> 6 = 37545

This suggests that your required result cannot be achieved by any similar right shift.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文