Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将5个整数组合成1_C#_Integer_Bit_Pack - Fatal编程技术网

C#将5个整数组合成1

C#将5个整数组合成1,c#,integer,bit,pack,C#,Integer,Bit,Pack,我正在尝试使用位移位操作将5个整数(每个最多999个)打包并解压为一个唯一的整数: static UInt64 Combine(uint a, uint b, uint c, uint d, uint e) { return (a << 48) | (b << 32 ) | (c << 16) | (d << 8) | e; } 静态UInt64联合收割机(uint a、uint b、uint c、uint d、u

我正在尝试使用位移位操作将5个整数(每个最多999个)打包并解压为一个唯一的整数:

static UInt64 Combine(uint a, uint b, uint c, uint d, uint e)
    {
        return (a << 48) | (b << 32 ) | (c << 16) | (d << 8) | e;
    }
静态UInt64联合收割机(uint a、uint b、uint c、uint d、uint e)
{

return(a为了打包值
0..999
,您需要10位,而不是8位。10位将为您提供值
0..1023
,而8位仅为您提供
0..255

因此,您需要的功能类似于:

static UInt64 Combine(
    uint a, uint b, uint c, uint d, uint e
) {
    UInt64 retval = a;
    retval = (retval << 10) | b;
    retval = (retval << 10) | c;
    retval = (retval << 10) | d;
    retval = (retval << 10) | e;
    return retval;
}

为了打包值
0..999
,您需要10位,而不是8位。10位将为您提供值
0..1023
,而8位仅为您提供
0..255

因此,您需要的功能类似于:

static UInt64 Combine(
    uint a, uint b, uint c, uint d, uint e
) {
    UInt64 retval = a;
    retval = (retval << 10) | b;
    retval = (retval << 10) | c;
    retval = (retval << 10) | d;
    retval = (retval << 10) | e;
    return retval;
}

另一种存储数字的方法。有点不同。但是,我想我会把它展示给你们。基本上,我们只是在模仿“工会”:


此结构仅存储4个值。但是,您可以使用更大的类型(而不是long)来存储更多的数字。

另一种存储数字的方法。它有点不同。但是,我想我会向您展示它。基本上,我们只是在模拟“联合”:


此结构仅存储4个值。但是,您可以使用更大的类型(而不是long)来保存更多的数字。

如果您不向我们展示您在尝试解包时所做的操作,我们如何告诉您所做的错误?您将需要10位来打包0-999。8位只能保存到255。10位将保存到1023。如果您不向我们展示您所做的操作,我们将如何告知您所做的错误若要尝试解包它们,我们如何才能告诉您做错了什么?您需要10位才能打包0-999。8位只能打包到255。10位可以打包到1023。
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Unions
{
    public partial class Form1 : Form
    {
        [StructLayout(LayoutKind.Explicit)]
        struct uShortArray
        {
            [FieldOffset(0)]
            public ushort Bytes01;
            [FieldOffset(2)]
            public ushort Bytes23;
            [FieldOffset(4)]
            public ushort Bytes45;
            [FieldOffset(6)]
            public ushort Bytes67;

            [FieldOffset(0)]
            public long long1;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            uShortArray ua = default(uShortArray);
            ua.Bytes01 = 999;
            ua.Bytes23 = 164;
            ua.Bytes45 = 581;
            ua.Bytes67 = 43;

            MessageBox.Show($"ua = [Bytes 0 - 1 : {ua.Bytes01}] ... [Byte 2 - 3 : {ua.Bytes23}] ... [Bytes 4 - 5 : {ua.Bytes45}] ... [Bytes 6 - 7 : {ua.Bytes67}] ... [long1 : {ua.long1}]");

            uShortArray ua2 = default(uShortArray);

            Combine(out ua2, 543, 657, 23, 999);

            MessageBox.Show($"ua2 = [Bytes 0 - 1 : {ua2.Bytes01}] ... [Byte 2 - 3 : {ua2.Bytes23}] ... [Bytes 4 - 5 : {ua2.Bytes45}] ... [Bytes 6 - 7 : {ua2.Bytes67}] ... [long1 : {ua2.long1}]");

            uShortArray ua3 = default(uShortArray);
            ua3.long1 = ua.long1;  //As you can see, you don't need an extract.  You just assign the "extract" value to long1.

            MessageBox.Show($"ua3 = [Bytes 0 - 1 : {ua3.Bytes01}] ... [Byte 2 - 3 : {ua3.Bytes23}] ... [Bytes 4 - 5 : {ua3.Bytes45}] ... [Bytes 6 - 7 : {ua3.Bytes67}] ... [long1 : {ua3.long1}]");
        }

        private void Combine(out uShortArray inUA, ushort in1, ushort in2, ushort in3, ushort in4)
        {
            inUA = default(uShortArray);

            inUA.Bytes01 = in1;
            inUA.Bytes23 = in2;
            inUA.Bytes45 = in3;
            inUA.Bytes67 = in4;
        }
    }
}