.NET - int32 平台的 MSB 是否不可知?

发布于 2024-10-09 05:11:53 字数 1069 浏览 11 评论 0原文

我有以下代码,用于从非负整数 Int32 获取 MSB(最高有效位),更具体地说:

private static readonly int[] powersOf2 = new int[]
                                        {
                                            1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
                                            32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304,
                                            8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912,
                                            1073741824
                                        };

public static int GetMsb(int value)
{
    for (int k = powersOf2.Length - 1; k >= 0; k--)
    {
        var bit = (value & powersOf2[k]) != 0;
        if (bit)
            return (k + 1);
    }
    return 0;
}

再次强调:假设该值不是负数。

我的问题是:
.NET 框架是否保证此代码可以在每个平台上正确运行:x86/Windows/Linux/Sun/64 位?

.NET 内部的 Int32 表示形式(包括 Endianness 和位/字节顺序)是否与平台无关?

预先感谢!
顺便说一句,如果这是重复的 - 请尽快发表评论。谢谢!

I have the following code for getting the MSB (Most Significant Bit) from a non-negative integer, Int32 to be more specific:

private static readonly int[] powersOf2 = new int[]
                                        {
                                            1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
                                            32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304,
                                            8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912,
                                            1073741824
                                        };

public static int GetMsb(int value)
{
    for (int k = powersOf2.Length - 1; k >= 0; k--)
    {
        var bit = (value & powersOf2[k]) != 0;
        if (bit)
            return (k + 1);
    }
    return 0;
}

Again: given that value is not negative.

My question is:
Does the .NET framework guarantee that this code would run appropriately on every platform: x86/Windows/Linux/Sun/64bit?

Is the Int32 representation inside .NET, including Endianness and bit/byte order, platform agnostic?

Thanks in advance!
BTW, if this is kind of a duplicate - please comment about it ASAP. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

月亮邮递员 2024-10-16 05:11:53

只要您将其视为int,是的,它与平台无关。这包括所有算术和按位(<<>> 等)运算。操作码始终确保它符合您的预期。

然而!如果您深入了解一下,它可能很重要;例如 BitConverter.GetBytes(int)BitConverter.ToInt32 关心字节顺序。您可以使用 BitConverter.IsLittleEndian 进行检查;在“常规”.NET 上它通常为 true,但在某些架构上的 IA64、XNA 或 Mono 上可能为 false

相同的逻辑适用于在 byte*int* 之间强制(例如)的任何不安全代码,或通过 构造的任何联合 [结构布局]

但在常规代码中,应该没问题。

As long as you treat it as an int, yes it is platform agnostic. This includes all arithmetic and bitwise (<< , >> etc) operations. The opcodes always make sure that it does what you expect.

However! If you peek below the covers, it might matter; for example BitConverter.GetBytes(int) and BitConverter.ToInt32 care about the endianness. You can check this with BitConverter.IsLittleEndian; it is usually true on "regular" .NET, but could be false perhaps on IA64, or XNA or Mono on some architectures.

The same logic applies to any unsafe code that coerces (for example) between byte* and int*, or any unions constructed via [StructLayout].

But in regular code, you should be fine.

尛丟丟 2024-10-16 05:11:53

字节顺序取决于平台,但这里的代码根本不依赖于字节顺序。

仅当您使用指针、联合(StructLayout:Explicit) 或 BitConverter 等低级内容时,字节序才会发挥作用。

整数类型之间的位移、整数算术和正常转换与字节序无关。

The endianness is platform dependent, but your code here doesn't depend on endianness at all.

Endianness only comes into play when you use low level stuff like pointers, unions(StructLayout:Explicit) or BitConverter.

Bitshifts, integer arithmetic and normal casts between integer types are endian agnostic.

怼怹恏 2024-10-16 05:11:53

你的代码将永远有效。

这并不是因为 Int32 的表示形式不会因平台的不同而改变,而是因为您的代码编写得足够好,无需依赖它:您正在将 Int32 与其他 Int32 进行 AND 运算。如果格式确实发生更改,则更改将同样影响您正在测试的数字以及 2 的幂表中的条目 - 因此代码仍然有效。

Your code will always work.

That's not because the representation of Int32 won't change from platform to platform, but because your code is well-written enough not to rely on it: you are ANDing Int32s with other Int32s. In the event that the format did change, the change would affect both the number you're testing, and the entries in your powers-of-2 table, equally - so the code would still work.

蛮可爱 2024-10-16 05:11:53

该代码是可移植的,但是,它返回 0 作为 int.MinValue 的 MSB,实际上是十六进制的 0x80000000,因为您正在使用有符号整数。我相信,这是一个适用于所有位的代码,并且不需要任何预先计算的值:

public static int GetMsb(int value)
{
    for(int i = 31; i >= 0; i--)
    {
        if ((value & 0x80000000) != 0) return i;
        value <<= 1;
    }
    return 0;
}

或使用 uint

public static int GetMsb(uint value)
{
    for(int i = 31; i >= 0; i--)
    {
        if ((value & 0x80000000) != 0) return i;
        value <<= 1;
    }
    return 0;
}

The code is portable, however, it returns 0 as the MSB for int.MinValue wich is in fact 0x80000000 in hexa, because you're working with signed integers. Here is a code that works for all bits, I believe, and does not need any precalculated values:

public static int GetMsb(int value)
{
    for(int i = 31; i >= 0; i--)
    {
        if ((value & 0x80000000) != 0) return i;
        value <<= 1;
    }
    return 0;
}

or with a uint:

public static int GetMsb(uint value)
{
    for(int i = 31; i >= 0; i--)
    {
        if ((value & 0x80000000) != 0) return i;
        value <<= 1;
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文