Open In App

Decimal.GetBits() Method in C#

Last Updated : 19 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Decimal.GetBits() Method is used to convert the value of a specified instance of Decimal to its equivalent binary representation.

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

Return Value: This method returns a 32-bit signed integer array with four elements that contain the binary representation of d.

Below programs illustrate the use of Decimal.GetBits() Method

Example 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]);
    }
}


Output:

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

Example 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]);
    }
}


Output:

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

Reference:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads