------------------------------------------------------------------ Converting to/from IEEE 754 single-precision floating point format ------------------------------------------------------------------ -Ian! D. Allen - idallen@idallen.ca - www.idallen.com For an online converter that lets you check your work, see: http://www.h-schmidt.net/FloatApplet/IEEE754.html Unless otherwise stated, convert using the IEEE 754 single precision format. The IEEE 754 single precision format is 32 bits, which are (from left to right): One bit for the sign; 8 bits for the exponent and; 23 bits for the mantissa/significand. The IEEE 754 double precision format is 64 bits, which are (from left to right): One bit for the sign; 11 bits for the exponent; 52 bits for the mantissa/significand. The conversions in this file only work for non-zero numbers. The IEEE 754 bit pattern for zero is 00000000h. Negative zero is 80000000h. EXAMPLE 1: Show that decimal 147.625 equals binary 4313A000h in IEEE 754 format. ---------- Step 1: Convert the decimal number to its binary fractional form. (Convert the parts on each side of the binary decimal point separately, using the techniques already learned in class.) The decimal number 147.625 converted to binary is: 10010011.101 Note that the implied binary exponent multiplier here is "1" (2**0). Step 2: Normalize the binary fractional number. Move the decimal point left or right so that only a single binary digit "1" is to the left of the binary decimal point. Compensate by adjusting the exponent in the opposite direction. 10010011.101 times 2**0 --> (move binary decimal point left 7) --> 1.0010011101 times 2**7 Moving the decimal left seven decreases the size of the number; so, we use an exponent of 7 to compensate and keep the number the same size. (Moving right would increase the size of the number, and the exponent would have to be negative, e.g. 0.001101 --> 1.101 times 2**(-3).) Step 3: Convert the exponent to 8-bit excess-127 notation. Add 127 to the exponent and convert it to 8-bit binary: 7 + 127 = 134 --> 10000110 ( = 128 + 4 + 2 ) Step 4: Convert the mantissa/significand to "hidden bit" format. Since every binary floating-point number (except zero!) is normalized with "1." at the start, there is no need to store that leftmost "1". Remove the leading "1." from the mantissa/significand: 1.0010011101 --> 0010011101 Step 5: Write down the 1+8+23 = 32 bits. 147.625 is positive - the sign bit is zero: 0 The next eight bits are the exponent: 10000110 The next 23 bits are the mantissa: 00100111010000000000000 Binary result (32 bits): 01000011000100111010000000000000 Step 6: Convert the 32 bits to hexadecimal, starting on the right. To convert 01000011000100111010000000000000 to hexadecimal, group the bits into chunks of four, starting from the right, and convert each four-bit chunk to its hexadecimal digit 0-9A-F: 0100 0011 0001 0011 1010 0000 0000 0000 Answer (hex): 4 3 1 3 A 0 0 0 Answer: 147.625 is 4313A000h in IEEE 754 single-precision format. EXAMPLE 2: Show that decimal 2004 equals binary 44FA8000h in IEEE 754 format. ---------- Step 1: decimal 2004 --> binary 11111010100.0 (no fractional part) Step 2: normalize binary 11111010100.0 --> 1.11110101000 times 2**10 We shifted the binary decimal point 10 places to the left, making the number smaller. The exponent needed to compensate is therefore 2**10 Step 3: convert exponent 10 --> 10 + 127 = 137 --> binary 10001001 (=128+8+1) Step 4: remove hidden digit from 1.11110101000 --> 11110101000 Step 5: Write down the 1+8+23 = 32 bits. 2004 is positive - the sign bit is zero: 0 The next eight bits are the exponent: 10001001 The next 23 bits are the mantissa: 11110101000000000000000 Binary result (32 bits): 01000100111110101000000000000000 Step 6: 0100 0100 1111 1010 1000 0000 0000 0000 4 4 F A 8 0 0 0 Answer: 2004 is 44FA8000h in IEEE 754 single-precision format. EXAMPLE 3: Show that decimal -20.5 equals binary C1A40000h in IEEE 754 format. ---------- Step 1: decimal 20.5 --> binary 10100.1 Step 2: normalize binary 10100.1 --> 1.01001 times 2**4 Step 3: exponent 4 + 127 = 131 = binary 10000011 Step 4: remove hidden digit from 1.01001 --> 01001 Step 5: -20.5 is negative - the sign bit is one: 1 The next eight bits are the exponent: 10000011 The next 23 bits are the mantissa: 010010000000000000000 Binary result (32 bits): 11000001101001000000000000000000 Step 6: 1100 0001 1010 0100 0000 0000 0000 0000 C 1 A 4 0 0 0 0 Answer: -20.5 is C1A40000h in IEEE 754 single-precision format. EXAMPLE 4: Show that decimal -0.5 equals binary BF000000h in IEEE 754 format. ---------- Step 1: decimal 0.5 --> binary 0.1 Step 2: normalize binary 0.1 --> 1.0 times 2**(-1) We shifted the binary decimal point 1 place to the right, making the number larger. The exponent needed to compensate is therefore 2**(-1) Step 3: exponent -1 + 127 = 126 = binary 01111110 Step 4: remove hidden digit from 1.0 --> 0 Step 5: -0.5 is negative - the sign bit is one: 1 The next eight bits are the exponent: 01111110 The next 23 bits are the mantissa: 000000000000000000000 Binary result (32 bits): 10111111000000000000000000000000 Step 6: 1011 1111 0000 0000 0000 0000 0000 0000 B F 0 0 0 0 0 0 Answer: -0.5 is BF000000h in IEEE 754 single-precision format. EXAMPLE 5: Show that decimal -1 equals binary BF800000h in IEEE 754 format. ---------- Step 1: decimal 1 --> binary 1.0 (no fractional part) Step 2: normalize binary 1.0 --> 1.0 times 2**0 The binary number 1.0 is already normalized. No need to shift. The exponent remains zero. Step 3: exponent 0 + 127 = 127 = binary 01111111 Step 4: remove hidden digit from 1.0 --> 0 Step 5: -1 is negative - the sign bit is one: 1 The next eight bits are the exponent: 01111111 The next 23 bits are the mantissa: 000000000000000000000 Binary result (32 bits): 10111111100000000000000000000000 Step 6: 1011 1111 1000 0000 0000 0000 0000 0000 B F 8 0 0 0 0 0 Answer: -0.5 is BF800000h in IEEE 754 single-precision format. EXAMPLE 6: Show that IEEE 754 binary 438F0000h is decimal 286 ---------- Step 6: Convert the hexadecimal into binary: 4 3 8 F 0 0 0 0 0100 0011 1000 1111 0000 0000 0000 0000 Step 5: Split the binary into 1+8+23 bit pieces: 01000011100011110000000000000000 --> 0 10000111 00011110000000000000000 Number is positive - the sign bit is zero: 0 The next eight bits are the exponent: 10000111 The next 23 bits are the mantissa: 00011110000000000000000 Step 4: add back the missing hidden digit to the mantissa/significand 00011110000000000000000 --> 1.00011110000000000000000 Step 3: convert the exponent to decimal and subtract 127 binary 10000111 (=128+4+2+1) = 135 --> 135 - 127 = 8 --> 2**8 Step 2: de-normalize the mantissa/significand (make exponent zero) 1.00011110000000000000000 times 2**8 --> (must move right 8 places) --> 100011110.000000000000000 times 2**0 We moved the binary decimal point 8 places to the right, making the number larger, which allowed us to reduce the exponent by the same amount (to zero). Step 1: binary 100011110.000000000000000 --> decimal 286 (=256+16+8+4+2) Answer: IEEE 754 binary 438F0000h is decimal 286. EXAMPLE 7: Show that IEEE 754 binary BF880000h is decimal -1.0625 ---------- Step 6: Convert the hexadecimal into binary: B F 8 8 0 0 0 0 1011 1111 1000 1000 0000 0000 0000 0000 Step 5: Split the binary into 1+8+23 bit pieces: 10111111100010000000000000000000 --> 1 01111111 00010000000000000000000 Number is negative - the sign bit is one: 1 The next eight bits are the exponent: 01111111 The next 23 bits are the mantissa: 00010000000000000000000 Step 4: add back the missing hidden digit to the mantissa/significand 00010000000000000000000 --> 1.00010000000000000000000 Step 3: convert the exponent to decimal and subtract 127 binary 01111111 = (2**7)-1 = 127 --> 127 - 127 = 0 --> 2**0 Step 2: de-normalize the mantissa/significand (make exponent zero) 1.00010000000000000000000 times 2**0 --> (no need to move decimal) --> 1.00010000000000000000000 times 2**0 An exponent of zero means no adjustment is needed. Step 1: binary 1.00010000000000000000000 --> decimal 1.0625 ( = 1 + 1*(2**(-4)) = 1 + 0.0625 = 1.0625 ) Answer: IEEE 754 binary BF880000h is decimal -1.0625 EXAMPLE 8: Show that decimal 128.5625 equals binary 43009000h in IEEE 754 format. ---------- Step 1: Convert the decimal number to its binary fractional form. (Convert the parts on each side of the binary decimal point separately, using the techniques already learned in class.) The decimal number 128.5625 converted to binary is: 10000000.1001 Note that the implied binary exponent multiplier here is "1" (2**0). Step 2: Normalize the binary fractional number. Move the decimal point left or right so that only a single binary digit "1" is to the left of the binary decimal point. Compensate by adjusting the exponent in the opposite direction. 10000000.1001 times 2**0 --> (move binary decimal point left 7) --> 1.00000001001 times 2**7 Moving the decimal left seven decreases the size of the number; so, we use an exponent of 7 to compensate and keep the number the same size. Step 3: Convert the exponent to 8-bit excess-127 notation. Add 127 to the exponent and convert it to 8-bit binary: 7 + 127 = 134 --> 10000110 ( = 128 + 4 + 2 ) Step 4: Convert the mantissa/significand to "hidden bit" format. Since every binary floating-point number (except zero!) is normalized with "1." at the start, there is no need to store that leftmost "1". Remove the leading "1." from the mantissa/significand: 1.00000001001 --> 00000001001 Step 5: Write down the 1+8+23 = 32 bits. 128.5625 is positive - the sign bit is zero: 0 The next eight bits are the exponent: 10000110 The next 23 bits are the mantissa: 00000001001000000000000 Binary result (32 bits): 01000011000000001001000000000000 Step 6: Convert the 32 bits to hexadecimal, starting on the right. To convert 01000011000000001001000000000000 to hexadecimal, group the bits into chunks of four, starting from the right, and convert each four-bit chunk to its hexadecimal digit 0-9A-F: 0100 0011 0000 0000 1001 0000 0000 0000 Answer (hex): 4 3 0 0 9 0 0 0 Answer: 128.5625 is 43009000h in IEEE 754 single-precision format. ---------------------------------------------------------------------------- *) The IEEE 754 floating-point number 81234567h is negative. Without converting, give the hexadecimal for the same number, only positive. Turn off the sign bit (8->0): 01234567h *) The IEEE 754 floating-point number 7EDCBA98h is positive. Without converting, give the hexadecimal for the same number, only negative. Turn on the sign bit (7->F): FEDCBA98h *) Without converting, circle all the IEEE 754 negative numbers: 1837A654h 7A6A3B65h 87B5CDE2h 90A5B5EFh A0000037h D1B8765Ah F0000000h Anything with the sign bit on is negative. (Choose anything starting with hex digits 8..F)