Decimal.GetBits() Method in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.