Printing IEEE 754

I'm working on an assignment but I'm stuck. For some reason I can't get this outcome:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162`` ``````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 to solve this problem but I can't figure out where I went wrong. The following code is my attempt for this project. I feel that I'm nearing the end but I can't finish.. Here is my code: #include #include static char *studentName = "name"; // report whether machine is big or small 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"); } } // get next int (entered in hex) using scanf() // returns 1 (success) or 0 (failure) // if call succeeded, return int value via i pointer int getNextHexInt(int *i) { // replace this code with the call to scanf() //*i = 0; //return 1; scanf ("%x", i); return 1; } // print requested data for the 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; } // do not change this function in any way int main(int argc, char **argv) { int i; // number currently being analyzed int nValues; // number of values successfully parsed by scanf printf("CS201 - A01p - %s\n\n", studentName); bigOrSmallEndian(); for (;;) { if (argc == 1) // allow grading script to control ... printf("> "); // ... whether prompt character is printed nValues = getNextHexInt(&i); printf("0x%08X\n", i); if (! nValues) { // encountered bad input printf("bad input\n"); while (getchar() != '\n') ; // flush bad line from input buffer continue; } printNumberData(i); if (i == 0) break; } printf("\n"); return 0; } ``````

My outcome for this code is:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````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 the problem lies in printNumberData but I can't point where..

I hope anyone can pitch in because any help is greatly appreciated.
Uh, what is the program's purpose?
Topic archived. No new replies allowed.