Decimal.GetBits() Method in C#
Last Updated :
19 Mar, 2019
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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
decimal value = 18446744073709551615M;
int [] arr = Decimal.GetBits(value);
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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
get (Decimal.MaxValue);
Console.WriteLine( "" );
get (Decimal.MinValue);
}
public static void get ( decimal value)
{
Console.WriteLine( "Converted value of {0} is" ,
value);
int [] arr = Decimal.GetBits(value);
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:
Share your thoughts in the comments
Please Login to comment...