Given the following code, package the four byte values ββin 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 - to a value so that it can be unpacked into the correct byte equivalent?
EDIT.
To clarify, if I try to multiply the value by another packed value
uint result = this.PackedValue * other.PackedValue
Then unzip 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 is a complete code example 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();
source share