You certainly heard people speaking about 32 bit or 64 bit computers. Now what does it mean to store a number in a 32 bit representation?
Instead of going very deep into it I am going to give an high level overview and some examples. There are enough websites out there with a lot of info when you want to dive real deep.
If a number is stored in 32 bits it means that there are 32 ones or zeros stored somewhere that represent your number.
1 bit is use as the sign bit to indicate whether the number is positive or negative.
8 bits are used to store the exponent of the number. This exponent is stored as 127 + the actual exponent of your number. It is read by subtracting 127 from the stored exponent.
The last 23 bits are what we call the mantisse. They represent the numbers after the comma for this floating point representation. The number before the comma is always 1 and therefore should not be stored. (now this is not true, there is an exception for representing the number 0,0 . But I will not go into that here)
Decimal to 32 bit floating point
How do you represent the number 5.5
- Well, 5.5 in binary is
- okay, normalize.
1.011 * 2
- Before the comma is always 1, so we drop that one and get the mantissa ->
- exponent is 2. We need to store this as 2 + 127 remember. That is 129 which in binary is
- The number is positive so the first bit should be a zero
- sign bit + exponent + mantissa =
1 1000 0001 01100000000000000000
32 bit floating point to decimal
What decimal number does this 32 bit floating point representation stands for:
- split in sign bit + exponent + mantissa =
0 10000101 01101000000000000000000
- first bit is a zero so the number is positive
- The exponent part is 133. That means 133 – 127 = 6. So 6 is the exponent of the normalized representation of the number we are looking for. -> 26
0110100000000000000000are numbers behind the comma so that means
- combining mantissa * exponent gives
1.01101 * 2
- Now convert
10110100= 2 + 8 + 16 + 64 = 90 !!
Now, what is the highest number that can be stored.
0 11111110 11111111111111111111111
- the first bit is ‘0’ so the number is positive
- the next 8 bits represent the decimal number 254. So 254 – 127 is the exponent. In decimal notation that means 2127 .
- Now let’s find the number:
- like we did above:
1.11111111111111111111111(remember number before comma is a one)
- Now, cause our exponent is 127, we move the comma 127 places to the right
- That is a giant number (I added underscores so you can count better) :
- If you would take the effort to convert this to decimal this gives you:
- Using smarter math:
- The last 23 bits are used to create the numbers behind the comma. These 23 numbers represent the number 223 . Now these numbers are calculated from the decimal notation as follows: 2 – 2–23. That is the number 1.9999998807907104.
- Bringing that all together we get:
- + 2254-127 * (2 – 2–23)
- 2127 * 1.9999998807907104 =
- like we did above: