This documentation is archived and is not being maintained.

Decimal.GetBits Method

Visual Studio 2010

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

```public static int[] GetBits(
decimal d
)
```

Parameters

d
Type: System.Decimal
The value to convert.

Return Value

Type: System.Int32[]
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 means 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.

Examples

The following code example uses the GetBits method to convert several Decimal values to their equivalent binary representations.

```
// Example of the decimal.GetBits method.
using System;

class DecimalGetBitsDemo
{
const string dataFmt = "{0,31}  {1,10:X8}{2,10:X8}{3,10:X8}{4,10:X8}";

// Display the decimal.GetBits argument and the result array.
public static void ShowDecimalGetBits( decimal Argument )
{
int[ ] Bits = decimal.GetBits( Argument );

Console.WriteLine( dataFmt, Argument,
Bits[ 3 ], Bits[ 2 ], Bits[ 1 ], Bits[ 0 ] );
}

public static void Main( )
{
Console.WriteLine( "This example of the " +
"decimal.GetBits( decimal ) method \ngenerates the " +
"following output. It displays the argument \nas a " +
"decimal and the result array in hexadecimal.\n" );
Console.WriteLine( dataFmt, "Argument", "Bits[3]",
"Bits[2]", "Bits[1]", "Bits[0]" );
Console.WriteLine( dataFmt, "--------", "-------",
"-------", "-------", "-------" );

// Get internal bits for decimal objects.
ShowDecimalGetBits( 1M );
ShowDecimalGetBits( 100000000000000M );
ShowDecimalGetBits( 10000000000000000000000000000M );
ShowDecimalGetBits( 100000000000000.00000000000000M );
ShowDecimalGetBits( 1.0000000000000000000000000000M );
ShowDecimalGetBits( 123456789M );
ShowDecimalGetBits( 0.123456789M );
ShowDecimalGetBits( 0.000000000123456789M );
ShowDecimalGetBits( 0.000000000000000000123456789M );
ShowDecimalGetBits( 4294967295M );
ShowDecimalGetBits( 18446744073709551615M );
ShowDecimalGetBits( decimal.MaxValue );
ShowDecimalGetBits( decimal.MinValue );
ShowDecimalGetBits( -7.9228162514264337593543950335M );
}
}

/*
This example of the decimal.GetBits( decimal ) method
generates the following output. It displays the argument
as a decimal and the result array in hexadecimal.

Argument     Bits[3]   Bits[2]   Bits[1]   Bits[0]
--------     -------   -------   -------   -------
1    00000000  00000000  00000000  00000001
100000000000000    00000000  00000000  00005AF3  107A4000
10000000000000000000000000000    00000000  204FCE5E  3E250261  10000000
100000000000000.00000000000000    000E0000  204FCE5E  3E250261  10000000
1.0000000000000000000000000000    001C0000  204FCE5E  3E250261  10000000
123456789    00000000  00000000  00000000  075BCD15
0.123456789    00090000  00000000  00000000  075BCD15
0.000000000123456789    00120000  00000000  00000000  075BCD15
0.000000000000000000123456789    001B0000  00000000  00000000  075BCD15
4294967295    00000000  00000000  00000000  FFFFFFFF
18446744073709551615    00000000  00000000  FFFFFFFF  FFFFFFFF
79228162514264337593543950335    00000000  FFFFFFFF  FFFFFFFF  FFFFFFFF
-79228162514264337593543950335    80000000  FFFFFFFF  FFFFFFFF  FFFFFFFF
-7.9228162514264337593543950335    801C0000  FFFFFFFF  FFFFFFFF  FFFFFFFF
*/

```

The following example uses the GetBits method to retrieve the component parts of an array. It then uses this array in the call to the Decimal constructor to instantiate a new Decimal value.

```
Module Example
Public Sub Main()
Dim values() As Decimal = { 1234.96d, -1234.96d }
For Each value In values
Dim parts() = Decimal.GetBits(value)
Dim sign As Boolean = (parts(3) And &h80000000) <> 0
Dim scale As Byte = CByte((parts(3) >> 16) And &H7F)

Dim newValue As New Decimal(parts(0), parts(1), parts(2), sign, scale)
Console.WriteLine("{0} --> {1}", value, newValue)
Next
End Sub
End Module
' The example displays the following output:
'    1234.96 --> 1234.96
'    -1234.96 --> -1234.96

```

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.