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
- 0 means positive number.
- 1 means negative number.
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.
- The corresponding binary number is (100011.1001 1001 1001 ...)2
- By normalizing the number we get (1.00011.1001 1001 1001 ... x 25)2
- We remove the leading 1 and put the part right of radix point in the mantissa bit.
- So, at the mantissa we put 00011 1001 1001 1001...
- Adding exponent 5 to bias 127 we get 132
- Binary of (132)10 is (10000100)2
- We put 10000100 at exponent place
- As (35.6)10 is positive we put 0 at place of sign bit
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?