Given the following code packing four byte
values into a uint
.
private static void Pack(byte x, byte y, byte z, byte w)
{
this.PackedValue = (uint)x |
((uint)y << 8) |
((uint)z << 16) |
((uint)w << 24);
}
Is it possible to apply mathematical operators like *, +, / and -
on the value in a manner that it can be unpacked into the correct byte
equivalent?
EDIT.
To clarify, if I attempt to multiply the value by another packed value
uint result = this.PackedValue * other.PackedValue
Then unpack using the following...
public byte[] ToBytes()
{
return new[]
{
(byte)(this.PackedValue & 0xFF),
(byte)((this.PackedValue >> 8) & 0xFF),
(byte)((this.PackedValue >> 16) & 0xFF),
(byte)((this.PackedValue >> 24) & 0xFF)
};
}
I get the wrong results.
Here's a full code sample showing the expected and actual result.
void Main()
{
uint x = PackUint(128, 128, 128, 128);
uint y = (uint)(x * 1.5f);
byte[] b1 = ToBytes(x);
x.Dump(); // 2155905152
b1.Dump(); // 128, 255, 128, 255 RIGHT!
byte[] b2 = ToBytes(y);
b2.Dump(); // 0, 192, 192, 192 WRONG! Should be 192, 192, 192, 192
}
// Define other methods and classes here
private static uint PackUint(byte x, byte y, byte z, byte w)
{
return ((uint)x) |
((uint)y << 8) |
((uint)z << 16) |
((uint)w << 24);
}
public static byte[] ToBytes(uint packed)
{
return new[]
{
(byte)(packed & 0xFF),
(byte)((packed >> 8) & 0xFF),
(byte)((packed >> 16) & 0xFF),
(byte)((packed >> 24) & 0xFF)
};
}
packing, in mathematics, a type of problem in combinatorial geometry that involves placement of figures of a given size or shape within another given figure—with greatest economy or subject to some other restriction.
Packing problems are a class of optimization problems in mathematics that involve attempting to pack objects together into containers. The goal is to either pack a single container as densely as possible or pack all objects using as few containers as possible.
A cuboid shape could sit on a table and pack with no wasted space. It would, however, be awkward to hold when taking a drink. Also, its edges represent weak points needing extra reinforcement.
The only reason it doesn't work for 1.5f
is because floats are not precise enough. Try 1.5d
(for double
) and your example will work. However this approach is limited to "nice" cases, i.e. those where the result in each byte is guaranteed to be a whole number. A special case is when you multiply by an integer, which will always work so long as none of the four results overflow.
It is also possible to do this for addition and subtraction provided that none of the individual bytes overflow. Obviously any overflow will mess up nearby bytes. This is particularly problematic if you wish to use 2's complement for negative bytes (-128 .. 127) because adding 3 to -2 is also an "overflow" and will mess up the next byte.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With