Decimal.GetBits 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| GetBits(Decimal) |
將指定實例 Decimal 的值轉換為其等價的二進位表示。 |
| GetBits(Decimal, Span<Int32>) |
將指定實例 Decimal 的值轉換為其等價的二進位表示。 |
GetBits(Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將指定實例 Decimal 的值轉換為其等價的二進位表示。
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()
參數
- d
- Decimal
要轉換的值。
傳回
一個32位元有號整數陣列,包含四個元素,包含的 d二進位表示。
範例
以下範例使用此 GetBits 方法將多個 Decimal 值轉換為其等價的二進位表示。 接著它會顯示方法回傳 GetBits 的陣列中元素的十進位值和十六進位值。
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
以下範例使用該 GetBits 方法來取得陣列的組成部分。 接著它會利用這個陣列在呼叫建 Decimal(Int32, Int32, Int32, Boolean, Byte) 構子時,實例化一個新的 Decimal 值。
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
備註
數字的二進位表示 Decimal 法由一個1位元符號、一個96位元整數,以及一個用於整數除法並指定其中哪部分是小數分數的縮放因子組成。 縮放因子隱含為數字10,指數範圍從0到28。
回傳值為一個由 32 位元帶符號整數組成的四元素陣列。
返回陣列的第一、第二和第三元素包含 96 位元整數中最低、中、高 32 位元。
返回陣列的第四個元素包含比例因子與符號。 它包含以下部分:
位元0到15,也就是下方的字,未被使用,必須為零。
第16到23位元必須包含0到28之間的指數,表示整數除以10的冪次方。
第 24 到 30 位元未被使用,必須為零。
第 31 位元包含符號:0 表示均值正,1 表示負值。
請注意,位元表示法區分負零與正零。 這些值在所有運算中都視為相等。
另請參閱
適用於
GetBits(Decimal, Span<Int32>)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將指定實例 Decimal 的值轉換為其等價的二進位表示。
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
參數
- d
- Decimal
要轉換的值。
傳回
4,即二進位表示中的整數數。
例外狀況
目的區間長度不足以儲存二進位表示。