Straight Dope Message Board > Main Computer question: little-endian and two's complements
 Register FAQ Calendar Mark Forums Read

#1
12-17-2004, 01:21 PM
 Skeptico Guest Join Date: Jul 2002
Computer question: little-endian and two's complements

Hello,

I'm trying to convert a 4-byte IEEE 745 floating point in an little-endian machine
(Intel) back into a format readable by humans. According to a little conversion program, the bit pattern (in hex):

80BEBF39 (with 80 at the lowest address)

corresponds to:

3.6572292e-04

I just can't seem to get that result when I convert by hand. I start by converting
the number back to big-endian (39BFBE80); then convert that to binary
(111001101111111011111010000000); and then isolate the fractional
part of the number, bits 9-31, (101111111011111010000000); and then
doing a reverse two's complement by inverting all bits and adding 1
(10000000100000110000000); and finally convert that number back to
decimal (4211072). As you can see, what I get is different from what
I'm expecting (36572292). Any programmers out there can help me?
#2
12-17-2004, 02:38 PM
 c_goat Guest Join Date: Aug 2000
According to this site, you took the wrong bits (bits 9-31 actually starts at the tenth bit), and you're converting the fractional part wrong.

39BFBE80 = 0011 1001 1011 1111 1011 1110 1000 0000

Break it into the 3 parts

Sign Bit (Bit 0) = 0
Exponent (Bits 1-8) = 0111 0011
Fractional (Bits 9-31) = 011 1111 1011 1110 1000 0000

The linked site says you add a "1." to the front of the fractional part, then convert it. Nothing about taking 2's complement.

I'll stop there because 1.01111111011111010000000 isn't a fun conversion. First position after the decimal is 1/2, next is 1/4, next is 1/8 etc.

On preview, you can put your decimal value into this site and you'll see the breakdown (use the rounded button to get your exact one).

 Bookmarks

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Main     About This Message Board     Comments on Cecil's Columns/Staff Reports     Straight Dope Chicago     General Questions     Great Debates     Elections     Cafe Society     The Game Room     In My Humble Opinion (IMHO)     Mundane Pointless Stuff I Must Share (MPSIMS)     Marketplace     The BBQ Pit Side Conversations     The Barn House

All times are GMT -5. The time now is 01:57 PM.