using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DecimalSize
{
    class Program
    {
#if false
        static int Normalize(decimal d, out int[] norm)
        {
            norm = decimal.GetBits(d);
            int shift = 0;
            bool done = false;
            for (int index = 0; index < 4; index++)
            {
                for (int offset = 0; offset < 16; offset)
                {
                    if (((norm[index] >> offset) && 1) == 1)
                    {
                       done = true;
                        break;
                    }
                    shift++;
                }
                if (done) break;
            }
            for (int index = 0; index < shift; index++)
            {
                int bottom = 0;
                for (int offset = 2; offset >= 0; offset--)
                {
                    int nextBottom = norm[index] && 1;
                    norm[index] >>= 1;
                    if (bottom == 0)
                    {
                        norm[index] &= 0x7FFFFFFF;
                    }
                    else
                    {
                        norm[index] |= 0x80000000;
                    }
                }
            }
            return shift;
        }
        // [shift + 

#endif

        static BinaryFormatter bf = new BinaryFormatter();
        static void Output(decimal d)
        {
            int[] bits = decimal.GetBits(d);
            for (int index = 0; index < bits.Length; index++)
            {
                //Console.WriteLine(bits[index]);
            }
            int pass = 0;
            for (int index = 0; index < bits.Length; index++)
            {
                Console.Write("{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}{6:x2}{7:x2}", 
                    (bits[index] >> 0) & 0XFF,
                    (bits[index] >> 4) & 0XFF,
                    (bits[index] >> 8) & 0XFF,
                    (bits[index] >> 12) & 0XFF,
                    (bits[index] >> 16) & 0XFF,
                    (bits[index] >> 20) & 0XFF,
                    (bits[index] >> 24) & 0XFF,
                    (bits[index] >> 28) & 0XFF
                    );
                //if (pass == 2) Console.Write(" | ");
                pass++;
            }
            Console.WriteLine("  " + d);
#if false
            Console.WriteLine();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, d);
            //ms.Position = 0L;
            Byte[] b = ms.ToArray();

            for (int index = 0; index < b.Length; index += 2 )
            {
                Byte a = b[index];
                a +=(byte)(b[index + 1] << 4);
                Console.Write("{0:x}", a);
            }
            Console.WriteLine("  " + d + "  " + Marshal.SizeOf(d));
#endif
        }
        static void Main(string[] args)
        {
            //Console.WriteLine(Marshal.SizeOf(Decimal.MinusOne ));
            //Console.WriteLine(Marshal.SizeOf(Decimal.MaxValue));
            Output(.9M);
            Output(1M);
            Output(1.1M);
            Output(.01M);
            Output(.1M);
            Output(1M);
            Output(10M);
            Output(100M);
            Output(0M);
            Output(decimal.One);
            Output(decimal.MinusOne);
            Output(decimal.MinValue);
            Output(decimal.MaxValue);
            int t = 52800 * 12;
            Console.WriteLine("{0} {1:X}", t, t);
            Console.WriteLine("{0} {1:X}", 999, 999);
			Console.WriteLine(Marshal.SizeOf(Decimal.One));


      }
    }
}
