C# Decimal GetBits

Description

Decimal GetBits converts the value of a specified instance of Decimal to its equivalent binary representation.

Syntax

Decimal.GetBits has the following syntax.


public static int[] GetBits(
  decimal d
)

Parameters

Decimal.GetBits has the following parameters.

  • d - The value to convert.

Returns

Decimal.GetBits method returns

Example

Example of the decimal.GetBits method.


/* w  w  w  .  ja  v  a 2s  .  com*/
using System;

class MainClass
{
    public static void ShowDecimalGetBits(decimal Argument)
    {
        int[] Bits = decimal.GetBits(Argument);

    }

    public static void Main()
    {
        ShowDecimalGetBits(1M);
        ShowDecimalGetBits(100000000000000M);
    }
}

The code above generates the following result.

Example 2

The following example uses the GetBits method to retrieve the component parts of an array. It then uses this array in the call to the Decimal(Int32, Int32, Int32, Boolean, Byte) constructor to instantiate a new Decimal value. using System;


using System;/* ww  w  .j av a2 s. com*/
public class Example
{
   public static void Main()
   {
      Decimal[] values = { 1234.96m, -1234.96m };
      foreach (var value in values) {
         int[] parts = Decimal.GetBits(value);
         bool sign = (parts[3] & 0x80000000) != 0;

         byte scale = (byte) ((parts[3] >> 16) & 0x7F); 
         Decimal newValue = new Decimal(parts[0], parts[1], parts[2], sign, scale);
         Console.WriteLine("{0} --> {1}", value, newValue);
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version