Binary representation of a .NET Decimal

后端 未结 2 1257
广开言路
广开言路 2020-12-09 16:53

How does a .NET decimal type get represented in binary in memory?

We all know how floating-point numbers are stored and the thusly the reasons for the

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 17:36

    https://www.csharpindepth.com/Articles/Decimal

    How is a decimal stored?

    A decimal is stored in 128 bits, even though only 102 are strictly necessary. It is convenient to consider the decimal as three 32-bit integers representing the mantissa, and then one integer representing the sign and exponent. The top bit of the last integer is the sign bit (in the normal way, with the bit being set (1) for negative numbers) and bits 16-23 (the low bits of the high 16-bit word) contain the exponent. The other bits must all be clear (0). This representation is the one given by decimal.GetBits(decimal) which returns an array of 4 ints.

提交回复
热议问题