I am writing various types to a byte stream by manually casting and shifting values. I have found this to be more than three times faster than using BitConverter or BinaryWriter.
My problem is with floats. I need to cast them to ints in order to perform shift operations on them, but any cast to int will cause an implicit conversion with truncation, etc. I want the keep the exact binary representation the same. Is this possible?
eg. I want to be able to do similar to:
byte[] bytes = new byte[4];
float myFloat = 32.2;
//following won't compile as can't shift floats.
bytes [0] = (byte)myFloat;
bytes [1] = (byte)(myFloat >> 8);
bytes [2] = (byte)(myFloat >> 16);
bytes [3] = (byte)(myFloat >> 24);