all groups > dotnet clr > september 2005 >
You're in the

dotnet clr

group:

Decimal Data Type bit layout


Decimal Data Type bit layout mdavidjohnson
9/9/2005 5:07:02 PM
dotnet clr:
What is the actual bit layout of the decimal data type?

i.e.; if the double data type layout is sign = 1 bit, biased exponent = 11
bits, and mantissa = 52 bits; what is the corresponding description for
decimal?

I read that a decimal includes a 1-bit sign, a 96-bit integer number, and an
exponent that ranges from 0 to 28 (which would require 5 bits). The total
would thus be 102 bits which would fit in 13 bytes with two bits left over.
RE: Decimal Data Type bit layout v-kevy NO[at]SPAM online.microsoft.com
9/10/2005 5:07:40 AM
Hi

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: Decimal Data Type bit layout Jon Skeet [C# MVP]
9/10/2005 7:03:45 AM
[quoted text, click to view]

That's exactly the layout - if you look at the docs for Decimal, it's
documented there.

I suspect the reason it doesn't go any further is that it's a lot
easier (and quicker) to manipulate three ints as the mantissa than
"three and a bit" ints. I suspect there aren't many applications for
which 28 digits is insufficient, but a couple more digits would be
okay.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Decimal Data Type bit layout mdavidjohnson
9/10/2005 8:10:03 AM
Thanks Jon,

I didn't express my question as clearly as I should have.

What I'm really looking for is an indication of how the bits are laid out in
a field of bytes. For example, a double is laid out in a field of 8 bytes
with the sign bit at the high end, followed by the 8-bit exponent, which is
followed by the 52-bit mantissa at the low end; all stored in little endian
order.

If the decimal type is really laid out in 13 bytes, where are the extra two
bits positioned?

I need this information so I can handle decimal types in assembly language
in a DLL.

[quoted text, click to view]
Re: Decimal Data Type bit layout mdavidjohnson
9/10/2005 8:11:06 AM
Sorry, I meant 11-bit exponent (I've got floats, doubles, and decimals all
swirling around in my head at the moment)

[quoted text, click to view]
Re: Decimal Data Type bit layout Jon Skeet [C# MVP]
9/10/2005 4:37:31 PM
[quoted text, click to view]

Look at Decimal.GetBits - there are 12 bytes of mantissa, then the last
four bytes contain 2 zero bytes, one byte with the exponent, and one
byte with just the sign.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Decimal Data Type bit layout Willy Denoyette [MVP]
9/10/2005 6:00:19 PM
The exact lay-out in BCL

32 bit flags
32 bit word high
32 bit word low
32 bit word mid
So 96 bits representing the number.

flags represent the scale and the sign, as follows:

31----24 - sign - number is negative when bit 31 = 1 (only one bit used)
23----16 - scale bits - number of decimal places (valid range 0-28, 5 bits
used)
fi. 12.365 is represented a 12365 with scale 3
remaining bits of flags are not used.


Willy.






[quoted text, click to view]

Re: Decimal Data Type bit layout Peter Sestoft
9/13/2005 10:04:02 AM
"Willy Denoyette [MVP]" <willy.denoyette@telenet.be> writes:

[quoted text, click to view]

This is the current Microsoft layout for decimal.

The third edition of the Ecma standard for CLI (2005) does not specify
the layout.

The reason is that Mike Cowlishaw at IBM UK has designed a clever
representation that offers more precision (34 decimal digits) and a
much larger exponent range (+/- 6134) within the same 128 bits. This
is likely to become the IEEE standard for decimal, and it is
conceivable that Microsoft will adopt this representation at some
point.

So for long-term portability, externally stored decimals should be
represented as strings rather than bit patterns.

Peter
--
Department of Natural Sciences http://www.dina.kvl.dk/~sestoft/
Royal Veterinary and Agricultural University * Tel +45 3528 2334
Re: Decimal Data Type bit layout v-jetan NO[at]SPAM online.microsoft.com (
9/20/2005 7:05:19 AM
Hi MDJ,

Does the community's replies make sense to you? If you still have any
concern, please feel free ot feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button