當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Decimal.GetBits()用法及代碼示例

Decimal.GetBits()方法用於將Decimal的指定實例的值轉換為其等效的二進製表示形式。

用法: public static int[] GetBits (decimal d);
Here, it takes the floating point value to convert.

返回值:此方法返回一個32位帶符號整數數組,其中包含四個包含d的二進製表示形式的元素。


下麵的程序演示了Decimal.GetBits()方法的使用

示例1:

// C# program to demonstrate the 
// Decimal.GetBits() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring and initializing value 
        decimal value = 18446744073709551615M; 
  
        // getting Equivalent bit 
        // using GetBits() method 
        int[] arr = Decimal.GetBits(value); 
  
        // Display the element 
        for (int i = 0; i < arr.Length; i++) 
            Console.WriteLine("Bit[{0}] = {1, 10:X8}", 
                                          i, arr[i]); 
    } 
}

輸出:

Bit[0] =   FFFFFFFF
Bit[1] =   FFFFFFFF
Bit[2] =   00000000
Bit[3] =   00000000

示例2:

// C# program to demonstrate the 
// Decimal.GetBits() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(Decimal.MaxValue); 
        Console.WriteLine(""); 
        get(Decimal.MinValue); 
    } 
  
    // defining get() method 
    public static void get(decimal value) 
    { 
  
        // getting Equivalent bit 
        // using GetBits() method 
        Console.WriteLine("Converted value of {0} is", 
                                               value); 
        int[] arr = Decimal.GetBits(value); 
  
        // Display the element 
        for (int i = 0; i < arr.Length; i++) 
            Console.WriteLine("Bit[{0}] = {1, 10:X8}", 
                                           i, arr[i]); 
    } 
}

輸出:

Converted value of 79228162514264337593543950335 is
Bit[0] =   FFFFFFFF
Bit[1] =   FFFFFFFF
Bit[2] =   FFFFFFFF
Bit[3] =   00000000

Converted value of -79228162514264337593543950335 is
Bit[0] =   FFFFFFFF
Bit[1] =   FFFFFFFF
Bit[2] =   FFFFFFFF
Bit[3] =   80000000

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Decimal.GetBits() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。