简体   繁体   中英

IEEE floating point representation

I have created following program to find the bit pattern of floating point no. but i got different then i calculated:

#include<stdio.h>

int main(void){
    float f = 1.234;
    char *ch;
    ch = (char *)(&f);
    printf("\n%d\n", *ch);
    ch++;
    printf("\n%d\n", *ch);
    ch++;
    printf("\n%d\n", *ch);
    ch++;
    printf("\n%d\n", *ch);

//  printf("%d %d %d %d", *ch, *(ch+1), *(ch+2), *(ch+3));
    printf("\n%f %e", f, f);
    return 0;
}

It gives me output:

-74

-13

-99

63

1.234000 1.234000e+00

What does it mean because i was expecting bit pattern as:

00111111 10111011 11100111 0110110

where i am wrong please correct me

I'm not sure where you got that bit pattern from.

For IEEE-754, 1.234 is equivalent to an underlying representation of 0x3F9DF3B6 (see eg http://babbage.cs.qc.edu/IEEE-754/Decimal.html ). So we have:

0x3F = 00111111 =  63
0x9D = 10011101 = -99 (as a signed char)
0xF3 = 11110011 = -13
0xB6 = 10110110 = -74

Depending on your system endianness, you might find that these bytes come up in the other order.

There is no binary printf format built into standard C. You'll need to write your own if that's the output format you want. You could get closer by using %x to see hexadecimal output; maybe that will get you what you need?

The bit pattern that you expect is wrong, it should be:

{"00111111", "10011101", "11110011", "10110110"} = {63, -99, -13, -74}

which produces exactly the numbers you got.

The sign, exponent and significant are: 0, 127, 1962934

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM