Decimal - GetBits
Converts the value of a specified instance of to its equivalent binary representation.
public static void Main() { // Define an array of Decimal values. Decimal[] values = { 1M, 2M, 10M, 1000M, 1000.00000000000000M, 1.0000000000000000000000000000M, 123456789M, 0.123456789M, 0.000000000123456789M, 0.000000000000000000123456789M, 4294967295M, 18446744073709551615M, Decimal.MaxValue, Decimal.MinValue, -7.9228162514264337593543950335M }; Console.WriteLine("{0,31} {1,10:X8}{2,10:X8}{3,10:X8}{4,10:X8}", "Argument", "Bits[3]", "Bits[2]", "Bits[1]","Bits[0]"); Console.WriteLine("{0,31} {1,10:X8}{2,10:X8}{3,10:X8}{4,10:X8}", "--------", "-------", "-------", "-------", "-------"); // Iterate each element and display its binary representation foreach (var value in values) { // C# Extension Method: Decimal - GetBits int[] bits = value.GetBits(); Console.WriteLine("{0,31} {1,10:X8}{2,10:X8}{3,10:X8}{4,10:X8}", value, bits[3], bits[2], bits[1], bits[0]); } }
View Source
using System; public static partial class Extensions { /// <summary> /// Converts the value of a specified instance of to its equivalent binary representation. /// </summary> /// <param name="d">The value to convert.</param> /// <returns>A 32-bit signed integer array with four elements that contain the binary representation of .</returns> public static Int32[] GetBits(this Decimal d) { return Decimal.GetBits(d); } }