c# - How to reinterpret cast a float to an int? Is there a non-static conversion operator or user-defined assignment operator for conversion on 'this'? -
1. how can reinterpret cast float int (or double long)?
float f = 2.0f; int = (int)f; // causes conversion
i want copy bit-pattern f
i
. how can done?
2. implicit , explicit operators in c# uses 1 intermediate object because operator function static
public static implicit operator myclass(double s) { return new myclass(s); } .. .. myclass m = 2.2; // code uses 'm' , 1 intermediate object.
this fine reference types, value-types big (say 20-30 bytes), cause unnecessary data copy. understanding correct? , if yes, why doesn't c# have non-static conversion operator or user-defined assignment operator conversion/assignment takes place on 'this'? if does, whats way it?
the bitconverter class can retrieve bytes primitive type, can use create int. option buffer.blockcopy if have large amounts of converting do.
float ff = 2.0f; int ii = bitconverter.toint32(bitconverter.getbytes(ff), 0); float[] ff = new float[...]; int[] ii = new int[ff.length]; buffer.blockcopy(ff, 0, ii, 0, ff.length * 4); // byte-wise copy of ff ii
no there no other option given in c#, however, think while you're correct in sense there copy made, sufficiently simple implementation have jit optimizations done, possibly removing need copy.
Comments
Post a Comment