C# Pack 5 integers into 1

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C# Pack 5 integers into 1



I'm trying to pack, and unpack, 5 integers ( max 999 for each ) into a single unique integer using bit shift operations:


static UInt64 Combine(uint a, uint b, uint c, uint d, uint e)

return (a << 48)



However, I am unable to unpack the number back.
Can anyone please guide me to what I could be doing wrong ?
thanks.





If you don't show us what you're doing to try to unpack them, how can we tell you what you're doing wrong?
– Ken White
Aug 12 at 3:38





You'll need 10 bits to pack 0-999. 8 bits only gets you to 255. 10 bits will get you to 1023.
– Flydog57
Aug 12 at 4:11




2 Answers
2



In order to pack the values 0..999, you need ten bits, not eight. Ten will give you the values 0..1023 whereas eight will only give you 0..255.


0..999


0..1023


0..255



So the function you need is something like:


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



Then, to unpack them, just extract each group of ten bits, one at a time, such as:


static void Extract(UInt64 val, out uint a, out uint b,
out uint c, out uint d, out uint e
)
e = Convert.ToUInt32(val & 0x3ff); val = val >> 10;
d = Convert.ToUInt32(val & 0x3ff); val = val >> 10;
c = Convert.ToUInt32(val & 0x3ff); val = val >> 10;
b = Convert.ToUInt32(val & 0x3ff); val = val >> 10;
a = Convert.ToUInt32(val & 0x3ff);



Another way of storing numbers. It's a little different. But, I thought I'd present it to you. Basically, we're just emulating "Unions":


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;





This struct only stores 4 values. But, you can use a larger type, instead of long, to hold more numbers.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS