将位数组转换为 uint 或类似的打包值

发布于 2024-11-02 15:47:35 字数 55 浏览 0 评论 0原文

我有大量布尔值,我想将它们打包/解包为 uint 或类似的值。我怎样才能在 C# 中做到这一点?

I have a large array of booleans, and I want to pack/unpack them into a uint or similar value. How can I do this in C#?

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

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

发布评论

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

评论(3

绝影如岚 2024-11-09 15:47:35

您可以使用 BitArray 类将 bool 数组转换为 int 数组:

int[] theIntArray = new int[(theBoolArray.Length + 31) / 32];
new BitArray(theBoolArray).CopyTo(theIntArray, 0);

You can use the BitArray class to convert the bool array into an int array:

int[] theIntArray = new int[(theBoolArray.Length + 31) / 32];
new BitArray(theBoolArray).CopyTo(theIntArray, 0);
北城孤痞 2024-11-09 15:47:35

您需要建立一系列位掩码(BIT0 到 BIT31,例如 2^0 和 2^32),然后使用按位运算符对它们进行操作:

// Initialise
byte value = 240;       // 11110000

// Clear bit 7
value &= 127;           // Result = 112 = 01110000

// Set bit 0
value |= 1;             // Result = 113 = 01110001

// Toggle bits 1, 3, 5 and 7
value ^= 170;           // Result = 219 = 11011011

或者您可以使用 BitArray 进行操作:

BitArray myBitArray = new BitArray(5); // Setup with length 5

myBitArray.SetAll(false); // Set all to false
myBitArray.Set(3, true); // Set element 3 on

bool is3Set = myBitArray.Get(3); // returns true
bool is4Set = myBitArray.Get(4); // returns false

阅读有关 BitArray 位于 MS Docs

在 C/C++ 中,位掩码可以这样写:

enum Bits
{
    BIT0 =      0x00000001,
    BIT1 =      0x00000002,
    BIT2 =      0x00000004,
    BIT3 =      0x00000008,
    BIT4 =      0x00000010,
    BIT5 =      0x00000020,
    BIT6 =      0x00000040,
    BIT7 =      0x00000080,
    BIT8 =      0x00000100,
    BIT9 =      0x00000200,
    BIT10 = 0x00000400,
    BIT11 = 0x00000800,
    BIT12 = 0x00001000,
    BIT13 = 0x00002000,
    BIT14 = 0x00004000,
    BIT15 = 0x00008000,
    BIT16 = 0x00010000,
    BIT17 = 0x00020000,
    BIT18 = 0x00040000,
    BIT19 = 0x00080000,
    BIT20 = 0x00100000,
    BIT21 = 0x00200000,
    BIT22 = 0x00400000,
    BIT23 = 0x00800000,
    BIT24 = 0x01000000,
    BIT25 = 0x02000000,
    BIT26 = 0x04000000,
    BIT27 = 0x08000000,
    BIT28 = 0x10000000,
    BIT29 = 0x20000000,
    BIT30 = 0x40000000,
    BIT31 = 0x80000000
};

You need to establish a series of bit masks (BIT0 to BIT31 e.g 2^0 and 2^32) then operate on them using bitwise operators:

// Initialise
byte value = 240;       // 11110000

// Clear bit 7
value &= 127;           // Result = 112 = 01110000

// Set bit 0
value |= 1;             // Result = 113 = 01110001

// Toggle bits 1, 3, 5 and 7
value ^= 170;           // Result = 219 = 11011011

or you can do it with BitArray's:

BitArray myBitArray = new BitArray(5); // Setup with length 5

myBitArray.SetAll(false); // Set all to false
myBitArray.Set(3, true); // Set element 3 on

bool is3Set = myBitArray.Get(3); // returns true
bool is4Set = myBitArray.Get(4); // returns false

read more about BitArray's here at MS Docs.

In C/C++, you bitmask may be written like this:

enum Bits
{
    BIT0 =      0x00000001,
    BIT1 =      0x00000002,
    BIT2 =      0x00000004,
    BIT3 =      0x00000008,
    BIT4 =      0x00000010,
    BIT5 =      0x00000020,
    BIT6 =      0x00000040,
    BIT7 =      0x00000080,
    BIT8 =      0x00000100,
    BIT9 =      0x00000200,
    BIT10 = 0x00000400,
    BIT11 = 0x00000800,
    BIT12 = 0x00001000,
    BIT13 = 0x00002000,
    BIT14 = 0x00004000,
    BIT15 = 0x00008000,
    BIT16 = 0x00010000,
    BIT17 = 0x00020000,
    BIT18 = 0x00040000,
    BIT19 = 0x00080000,
    BIT20 = 0x00100000,
    BIT21 = 0x00200000,
    BIT22 = 0x00400000,
    BIT23 = 0x00800000,
    BIT24 = 0x01000000,
    BIT25 = 0x02000000,
    BIT26 = 0x04000000,
    BIT27 = 0x08000000,
    BIT28 = 0x10000000,
    BIT29 = 0x20000000,
    BIT30 = 0x40000000,
    BIT31 = 0x80000000
};
夕色琉璃 2024-11-09 15:47:35

这并不完全相同,但请检查此答案,它将布尔数组转换为字节:

Convert bool [] 到字节[]

This isn't the exact same but check this answer which converts an array of booleans to bytes:

Convert bool[] to byte[]

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