当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。