Hi, folks!
I read somewhere that the decimal variable type uses 128-bits in
memory for each variable. However, I read from other sources that the
decimal type only uses 96-bits to store the number.
Does anyone know the real number and why there might be a discrepancy?
TIA...
Paul E Collins - 12 Oct 2007 17:50 GMT
> I read somewhere that the decimal variable type
> uses 128-bits in memory for each variable.
> However, I read from other sources that the
> decimal type only uses 96-bits to store the number.
So perform the experiment yourself:
Console.WriteLine(sizeof(decimal) * 8);
Eq.
Kevin Spencer - 12 Oct 2007 18:01 GMT
This is from the MSDN Library (so you can believe it, rather than something
you "read somewhere"):
"The binary representation of a Decimal value consists of a 1-bit sign, a
96-bit integer number, and a scaling factor used to divide the 96-bit
integer and specify what portion of it is a decimal fraction. The scaling
factor is implicitly the number 10, raised to an exponent ranging from 0 to
28. "

Signature
HTH,
Kevin Spencer
Microsoft MVP
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
> Hi, folks!
>
[quoted text clipped - 5 lines]
>
> TIA...
Jon Skeet [C# MVP] - 12 Oct 2007 18:57 GMT
> I read somewhere that the decimal variable type uses 128-bits in
> memory for each variable. However, I read from other sources that the
> decimal type only uses 96-bits to store the number.
>
> Does anyone know the real number and why there might be a discrepancy?
Decimal uses 96 bits to store the mantissa, 5 bits to store the
exponent, and 1 bit to store the sign. However, it's stored as a 128
bit value. So yes, 26 bits are wasted - but it would be hard to put
them to good use.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too