codeomnitrix
codeomnitrix

Reputation: 4249

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

Upvotes: 2

Views: 834

Answers (3)

Norbert P.
Norbert P.

Reputation: 2807

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

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272447

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 e.g. 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.

Upvotes: 5

Carl Norum
Carl Norum

Reputation: 224834

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?

Upvotes: 1

Related Questions