Reputation: 744
In case of float
, IEEE-754 32-bit single precision format is used to store number in computer hardware.
Here the most significant bit i.e. bit 31 is used to represent sign. It is either 0 or 1
The next 8 bits i.e. from bit 30 to 23 is used to represent exponent. It has a bias of 127.
The next 23 bits i.e. from bit 22 to 0 is used to represent mantissa.
For example we have the number (35.6)10.
Thus the hardware level representation of (35.6)10 is
Double numbers are represented using IEEE-754 64-bit double-precision format.
Here the most significant bit i.e. the bit 63 is used to represent sign. It is either 0 or 1.
The next 11 bits i.e. from bit 62 to 52 is used to represent exponent. It has a bias of 1023.
The next 52 bits i.e. from bit 51 to 0 is used to represent mantissa.
Similarly how decimal data type or fixed-point number is stored in computer hardware?
Upvotes: 1
Views: 1758
Reputation: 41753
Fixed-point numbers are just series of bits in positional notation with their contribution to the final value predetermined, i.e. the position of the radix point is known at an exact place. Integers are just a subset of fixed-point numbers, with the radix point put on the right of the last bit (position 0)
That means fixed-point numbers are stored just as integers. However after each operation a shift (and possibly some other roundings) need to be made to move the radix point to the original position instead of position 0. Some architectures (most commonly DSPs) have hardware support for that, but most don't and the programmer is responsible for doing that
OTOH decimal numbers are not related to fixed-point numbers in anyway. They're just numbers in a different base (decimal instead of binary). Depending whether the radix point is fixed or movable we'll have fixed-point or floating-point decimal values. If it's floating then the exponent will be stored separately just like binary floating-point values
In case of floating-point decimal type, the decimal value's significand part is also an integer and there are various ways to store it. The simplest way is to store each digit in a byte. This is wasteful on memory and maybe slow to operate on in many cases. Another common encoding is BCD which stores a decimal digit in a nibble (since a nibble has 16 different states and is enough for 10 values). For packing the digits even more there's DPD which very cleverly stores 3 decimal digits in 10 bits (uses 1000 of the 1024 different states). This method is used in IEEE-754's decimal floating-point formats
Upvotes: 0
Reputation: 648
Fixed-point numbers are stored just like integers. The hardware does not maintain any information about the location of the binary point, just as the hardware doesn't now whether a stored value is signed or unsigned. It's up to the programmer or compiler to keep track of the fixed-point format of the variables and perform the correct operations on them.
It's not clear to me what you mean by a "decimal data type". If you are referring to BCD, then the data is again stored as an integer and the programmer/compiler has the responsibility of operating on it appropriately.
Upvotes: 0