Bagikan melalui


Decimal.GetBits Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function GetBits ( _
    d As Decimal _
) As Integer()
public static int[] GetBits(
    decimal d
)

Parameters

Return Value

Type: array<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 meaning positive, and 1 meaning 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. 

Module Example

   Const dataFmt As String = _
       "{0,31}  {1,10:X8}{2,10:X8}{3,10:X8}{4,10:X8}"

   ' Display the Decimal.GetBits argument and the result array.
   Sub ShowDecimalGetBits(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Argument As Decimal)

      Dim Bits As Integer() = Decimal.GetBits(Argument)

      outputBlock.Text &= String.Format(dataFmt, Argument, _
          Bits(3), Bits(2), Bits(1), Bits(0)) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= String.Format("This example of the " & _
          "Decimal.GetBits( Decimal ) method " & vbCrLf & _
          "generates the following output. It displays " & _
          "the argument " & vbCrLf & "as a Decimal and the result " & _
          "array in hexadecimal." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "Argument", "Bits(3) & vbCrLf", _
          "Bits(2)", "Bits(1)", "Bits(0)")
      outputBlock.Text &= String.Format(dataFmt, "--------", "-------", _
          "-------", "-------", "-------") & vbCrLf

      ' Get internal bits for Decimal objects.
      ShowDecimalGetBits(outputBlock, 1D)
      ShowDecimalGetBits(outputBlock, 100000000000000D)
      ShowDecimalGetBits(outputBlock, 10000000000000000000000000000D)
      ShowDecimalGetBits(outputBlock,  _
          Decimal.Parse("100000000000000.00000000000000"))
      ShowDecimalGetBits(outputBlock,  _
          Decimal.Parse("1.0000000000000000000000000000"))
      ShowDecimalGetBits(outputBlock, 123456789D)
      ShowDecimalGetBits(outputBlock, 0.123456789D)
      ShowDecimalGetBits(outputBlock, 0.000000000123456789D)
      ShowDecimalGetBits(outputBlock, 0.000000000000000000123456789D)
      ShowDecimalGetBits(outputBlock, 4294967295D)
      ShowDecimalGetBits(outputBlock, 18446744073709551615D)
      ShowDecimalGetBits(outputBlock, Decimal.MaxValue)
      ShowDecimalGetBits(outputBlock, Decimal.MinValue)
      ShowDecimalGetBits(outputBlock, -7.9228162514264337593543950335D)
   End Sub
End Module

' 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
// Example of the decimal.GetBits method. 
using System;

class Example
{
   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(System.Windows.Controls.TextBlock outputBlock, decimal Argument)
   {
      int[] Bits = decimal.GetBits(Argument);

      outputBlock.Text += String.Format(dataFmt, Argument,
          Bits[3], Bits[2], Bits[1], Bits[0]) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("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") + "\n";
      outputBlock.Text += String.Format(dataFmt, "Argument", "Bits[3]",
          "Bits[2]", "Bits[1]", "Bits[0]") + "\n";
      outputBlock.Text += String.Format(dataFmt, "--------", "-------",
          "-------", "-------", "-------") + "\n";

      // Get internal bits for decimal objects.
      ShowDecimalGetBits(outputBlock, 1M);
      ShowDecimalGetBits(outputBlock, 100000000000000M);
      ShowDecimalGetBits(outputBlock, 10000000000000000000000000000M);
      ShowDecimalGetBits(outputBlock, 100000000000000.00000000000000M);
      ShowDecimalGetBits(outputBlock, 1.0000000000000000000000000000M);
      ShowDecimalGetBits(outputBlock, 123456789M);
      ShowDecimalGetBits(outputBlock, 0.123456789M);
      ShowDecimalGetBits(outputBlock, 0.000000000123456789M);
      ShowDecimalGetBits(outputBlock, 0.000000000000000000123456789M);
      ShowDecimalGetBits(outputBlock, 4294967295M);
      ShowDecimalGetBits(outputBlock, 18446744073709551615M);
      ShowDecimalGetBits(outputBlock, decimal.MaxValue);
      ShowDecimalGetBits(outputBlock, decimal.MinValue);
      ShowDecimalGetBits(outputBlock, -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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.