c - Analyzing IEEE 754 bit patterns -



c - Analyzing IEEE 754 bit patterns -

i'm working on assignment i'm stuck. reason can't outcome:

byte order: little-endian > ffffffff 0xffffffff signbit 1, expbits 255, fractbits 0x007fffff qnan > 3 0x00000003 signbit 0, expbits 0, fractbits 0x00000003 denormalized: exp = -126 > deadbeef 0xdeadbeef signbit 1, expbits 189, fractbits 0x002dbeef normalized: exp = 62 > deadbeef 0xdeadbeef signbit 1, expbits 189, fractbits 0x002dbeef normalized: exp = 62 > 0 0x00000000 signbit 0, expbits 0, fractbits 0x00000000 +zero

i have tried solve problem can't figure out went wrong. next code effort project. sense i'm nearing end can't finish..

here code:

#include <stdio.h> #include <stdlib.h> static char *studentname = "name"; // study whether machine big or little endian void bigorsmallendian() { int num = 1; if(*(char *)&num == 1) { printf("\nbyte order: little-endian\n\n"); } else { printf("\nbyte order: big-endian\n\n"); } } // next int (entered in hex) using scanf() // returns 1 (success) or 0 (failure) // if phone call succeeded, homecoming int value via pointer int getnexthexint(int *i) { // replace code phone call scanf() //*i = 0; //return 1; scanf ("%x", i); homecoming 1; } // print requested info given number void printnumberdata(int i) { //printf("%x %0#10x\n",i,*(int *)&i); int tru_exp =0; //int stored_exp; int negative; int exponent; int mantissa; printf("\n>"); printf("\n0x%08x",i); negative = !!(i & 0x80000000); exponent = (i & 0x7f800000) >> 23; mantissa = (i & 0x007fffff); printf("\nsignbit %d, ", negative); printf("expbits %d, ", exponent); printf("fractbits 0x%08x", mantissa); // "%#010x, ", mantissa); if(exponent == 0) { if(mantissa != 0) { printf("\ndenormalized "); } } else{ printf("\nnormalized: "); tru_exp = exponent - 127; printf("exp = %d", tru_exp); } if(exponent == 0 && mantissa == 0 && negative == 1) { printf("\n-zero"); } if(exponent ==0 && mantissa == 0 && negative == 0) { printf("\n+zero"); } if(exponent == 255 && mantissa != 0 && negative == 1) { printf("\nqnan"); } if(exponent == 255 && mantissa != 0 && negative == 0) { printf("\nsnan"); } if(exponent == 0xff && mantissa == 0 && negative == 1) { printf("\n-infinity"); } if(exponent == 0xff && mantissa == 0 && negative == 0) { printf("\n+infinity"); } printf("\n"); while(i != 0) break; } // not alter function in way int main(int argc, char **argv) { int i; // number beingness analyzed int nvalues; // number of values parsed scanf printf("cs201 - a01p - %s\n\n", studentname); bigorsmallendian(); (;;) { if (argc == 1) // allow grading script command ... printf("> "); // ... whether prompt character printed nvalues = getnexthexint(&i); printf("0x%08x\n", i); if (! nvalues) { // encountered bad input printf("bad input\n"); while (getchar() != '\n') ; // flush bad line input buffer continue; } printnumberdata(i); if (i == 0) break; } printf("\n"); homecoming 0; }

my outcome code is:

byte order: little-endian > ffffffff 0xffffffff > 0xffffffff signbit 1, expbits 255, fractbits 0x007fffff normalized: exp = 128 qnan > 3 0x00000003 > 0x00000003 signbit 0, expbits 0, fractbits 0x00000003 denormalized > deadbeef 0xdeadbeef > 0xdeadbeef signbit 1, expbits 189, fractbits 0x002dbeef normalized: exp = 62 > deadbeef 0xdeadbeef > 0xdeadbeef signbit 1, expbits 189, fractbits 0x002dbeef normalized: exp = 62 > 0 0x00000000 > 0x00000000 signbit 0, expbits 0, fractbits 0x00000000 +zero

i believe problem lies in printnumberdata() can't point where.

i hope can pitch in because help appreciated.

so, problem, far see, exponent isn't signed. can prepare changing tru_exp int8_t (or signed char). or sign-extending number manually, e.g. if (tru_exp > 128) tru_exp -= 256;

c floating-point ieee-754

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -