Decimal.GetBits Metodo

Definizione

Overload

GetBits(Decimal)

Converte il valore dell'istanza specificata di Decimal nella rappresentazione binaria equivalente.

GetBits(Decimal, Span<Int32>)

Converte il valore dell'istanza specificata di Decimal nella rappresentazione binaria equivalente.

GetBits(Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converte il valore dell'istanza specificata di Decimal nella rappresentazione binaria equivalente.

public:
 static cli::array <int> ^ GetBits(System::Decimal d);
public static int[] GetBits (decimal d);
static member GetBits : decimal -> int[]
Public Shared Function GetBits (d As Decimal) As Integer()

Parametri

d
Decimal

Valore da convertire.

Restituisce

Int32[]

Matrice di intero con segno a 32 bit con quattro elementi che contengono la rappresentazione binaria di d.

Esempio

Nell'esempio seguente viene usato il GetBits metodo per convertire diversi Decimal valori nelle rappresentazioni binarie equivalenti. Visualizza quindi i valori decimali e il valore esadecimale degli elementi nella matrice restituita dal GetBits metodo .

using namespace System;

int main()
{
   // Define an array of Decimal values.
   array<Decimal>^ values = gcnew array<Decimal>  { Decimal::One, 
                                  Decimal::Parse("100000000000000"), 
                                  Decimal::Parse("10000000000000000000000000000"),
                                  Decimal::Parse("100000000000000.00000000000000"), 
                                  Decimal::Parse("1.0000000000000000000000000000"),
                                  Decimal::Parse("123456789"), 
                                  Decimal::Parse("0.123456789"), 
                                  Decimal::Parse("0.000000000123456789"), 
                                  Decimal::Parse("0.000000000000000000123456789"), 
                                  Decimal::Parse("4294967295.0"), 
                                  Decimal::Parse("18446744073709551615.0"), 
                                  Decimal::MaxValue, Decimal::MinValue, 
                                  Decimal::Parse("-7.9228162514264337593543950335") }; 

   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}", 
                      "--------", "-------", "-------", "-------", "-------" );
   
   for each (Decimal value in values)
   {
      array<int>^ bits = Decimal::GetBits(value); 
      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] );
   }
}

/*
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
*/
using System;

class Example
{
   public static void Main()
   {
      // Define an array of Decimal values.
      Decimal[] values = { 1M, 100000000000000M, 10000000000000000000000000000M,
                           100000000000000.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) {
        int[] bits = decimal.GetBits(value);
        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]);
      }
   }
}
// The example displays the following output:
//                           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
open System

// Define an list of Decimal values.
let values = 
    [ 1M; 100000000000000M; 10000000000000000000000000000M
      100000000000000.00000000000000M; 1.0000000000000000000000000000M
      123456789M; 0.123456789M; 0.000000000123456789M
      0.000000000000000000123456789M; 4294967295M
      18446744073709551615M; Decimal.MaxValue
      Decimal.MinValue; -7.9228162514264337593543950335M ]

printfn $"""{"Argument",31}  {"Bits[3]",10:X8}{"Bits[2]",10:X8}{"Bits[1]",10:X8}{"Bits[0]",10:X8}"""
printfn $"""{"--------",31}  {"-------",10:X8}{"-------",10:X8}{"-------",10:X8}{"-------",10:X8}"""

// Iterate each element and display its binary representation
for value in values do
   let bits = Decimal.GetBits value
   printfn $"{value,31}  {bits[3],10:X8}{bits[2],10:X8}{bits[1],10:X8}{bits[0],10:X8}"


// The example displays the following output:
//                           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
Module Example
   Public Sub Main()
      ' Define an array of decimal values.
      Dim values() As Decimal = { 1d, 100000000000000d, 
                                  10000000000000000000000000000d,
                                  100000000000000.00000000000000d, 
                                  1.0000000000000000000000000000d,
                                  123456789d, 0.123456789d, 
                                  0.000000000123456789d,
                                  0.000000000000000000123456789d, 
                                  4294967295d,
                                  18446744073709551615d, 
                                  Decimal.MaxValue, Decimal.MinValue, 
                                  -7.9228162514264337593543950335d }

      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
      For Each value In values
         Dim bits() As Integer = Decimal.GetBits(value)
        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))

       Next
    End Sub
End Module 
' The example displays the following output:
'
'                        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

Nell'esempio seguente viene usato il GetBits metodo per recuperare le parti componente di una matrice. Usa quindi questa matrice nella chiamata al Decimal(Int32, Int32, Int32, Boolean, Byte) costruttore per creare un'istanza di un nuovo Decimal valore.

using System;

public class Example
{
   public static void Main()
   {
      Decimal[] values = { 1234.96m, -1234.96m };
      foreach (var value in values) {
         int[] parts = Decimal.GetBits(value);
         bool sign = (parts[3] & 0x80000000) != 0;

         byte scale = (byte) ((parts[3] >> 16) & 0x7F);
         Decimal newValue = new Decimal(parts[0], parts[1], parts[2], sign, scale);
         Console.WriteLine("{0} --> {1}", value, newValue);
      }
   }
}
// The example displays the following output:
//       1234.96 --> 1234.96
//       -1234.96 --> -1234.96
open System

let values = [ 1234.96m; -1234.96m ]
for value in values do
    let parts = Decimal.GetBits value
    let sign = (parts[3] &&& 0x80000000) <> 0

    let scale = (parts[3] >>> 16) &&& 0x7F |> byte
    let newValue = Decimal(parts[0], parts[1], parts[2], sign, scale)
    printfn $"{value} --> {newValue}"

// The example displays the following output:
//       1234.96 --> 1234.96
//       -1234.96 --> -1234.96
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

Commenti

La rappresentazione binaria di un Decimal numero è costituita da un segno a 1 bit, un numero intero a 96 bit e un fattore di ridimensionamento utilizzato per dividere il numero intero e specificare la parte di cui è una frazione decimale. Il fattore di ridimensionamento è implicitamente il numero 10, generato a un esponente compreso tra 0 e 28.

Il valore restituito è una matrice a quattro elementi di interi con segno a 32 bit.

Il primo, secondo e il terzo elementi della matrice restituita contengono i bit bassi, intermedi e alti 32 bit del numero intero a 96 bit.

Il quarto elemento della matrice restituita contiene il fattore di scala e il segno. È costituito dalle parti seguenti:

I bit da 0 a 15, la parola inferiore, sono inutilizzati e devono essere zero.

I bit da 16 a 23 devono contenere un esponente compreso tra 0 e 28, che indica la potenza di 10 per dividere il numero intero.

I bit da 24 a 30 sono inutilizzati e devono essere zero.

Bit 31 contiene il segno: 0 media positivo e 1 significa negativo.

Si noti che la rappresentazione bit distingue tra zero negativo e positivo. Questi valori vengono considerati uguali in tutte le operazioni.

Vedi anche

Si applica a

GetBits(Decimal, Span<Int32>)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converte il valore dell'istanza specificata di Decimal nella rappresentazione binaria equivalente.

public:
 static int GetBits(System::Decimal d, Span<int> destination);
public static int GetBits (decimal d, Span<int> destination);
static member GetBits : decimal * Span<int> -> int
Public Shared Function GetBits (d As Decimal, destination As Span(Of Integer)) As Integer

Parametri

d
Decimal

Valore da convertire.

destination
Span<Int32>

Intervallo in cui archiviare la rappresentazione binaria dei quattro valori Integer.

Restituisce

4, ovvero il numero di valori Integer nella rappresentazione binaria.

Eccezioni

La lunghezza dell'intervallo di destinazione non era sufficiente per archiviare la rappresentazione binaria.

Si applica a