using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Explicit)] struct Float64 { public Float64(double x) { ULong = 0; Double = x; } public static implicit operator Float64(double x) { return new Float64(x); } public static implicit operator double(Float64 x) { return x.Double; } [FieldOffset(0)] public double Double; [FieldOffset(0)] public ulong ULong; public ulong Mantissa { get { return ULong & 0xFFFFFFFFFFFFF; } set { ULong = ULong & ~0xFFFFFFFFFFFFFULL | value & 0xFFFFFFFFFFFFFULL; } } public uint Exp { get { return (uint)((ULong >> 52) & 0x7FF); } set { ULong = ULong & 0x800FFFFFFFFFFFFFULL | ((ulong)(value & 0x7FF) << 52); } } public uint Sign { get { return (uint)(ULong >> 63); } set { ULong = ULong & 0x7FFFFFFFFFFFFFFFULL | ((ulong)value << 63); } } } public class Test { private static void Print(Float64 x) { Console.WriteLine("{0,7} {1:X16} s={4} m={2:X13} e={3:X3}", x.Double, x.ULong, x.Mantissa, x.Exp, x.Sign); } public static void Main() { foreach (var d in new double [] { 0,1,-1,2,-2,3,-3,3.25,-3.25 }) Print(d); Float64 x = 1; Print(x); ++x.Exp; Print(x); // 2 x.Sign = 1; Print(x); // -2 x.Mantissa += 1ULL << 51; Print(x); // -3 --x.Exp; Print(x); // -1.5 } }