System.Decimal.GetBits Method

Converts the value of a specified instance of decimal to its equivalent binary representation.

Syntax

public static int[] GetBits (decimal d)

Parameters

d
The value to convert.

Returns

A 32-bit signed integer array with four elements that contain the binary representation of d.

Remarks

The binary representation of a decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

The return value is a four-element array of 32-bit signed integers.

The first, second, and third elements of the returned array contain the low, middle, and high 32 bits of the 96-bit integer number.

The fourth element of the returned array contains the scale factor and sign. It consists of the following parts:

Bits 0 to 15, the lower word, are unused and must be zero.

Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.

Bits 24 to 30 are unused and must be zero.

Bit 31 contains the sign: 0 mean positive, and 1 means negative.

Note that the bit representation differentiates between negative and positive zero. These values are treated as being equal in all operations.

Example

The following example demonstrates the different representations of 1.00 and 1.

C# Example

using System;
public class Class1  {
    public static void Print (int [] bs) {
        foreach (int b in bs) {
            Console.Write (b+" ");
        }
    }
public static void Main () {
   decimal d = 1.00m;
    decimal d1 = 1;
    Console.Write (d);
    Console.Write (" - bits: "); 
    Print (decimal.GetBits(d));
    Console.WriteLine();
    Console.Write (d1);
    Console.Write (" - bits: "); 
    Print (decimal.GetBits(d1));
    Console.WriteLine();
    Console.WriteLine ("d1.CompareTo(d) == {0}", d1.CompareTo(d));
    Console.WriteLine ("d1 == d {0}", d1 == d);
}
}
   

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0