Hi there, I'm currently working on a project that requires for me to print out data about a hexadecimal. It needs to print out signBit, expBit, and fracBits.

I have tried to look onine but I haven't had any luck finding something useful.

These are the requirements:

Your code will interpret those 8 digits as an IEEE 754 32-bit floating point number and will print out information about that number. Do not worry about bad input. My test script does not do this.

Use scanf to get the user input. Your code should accept input with or without "0x" in front of it (scanf does this).

For each number, your code should print out:

the sign bit
the exponent bits, expressed as a decimal number
the fraction bits, expressed as an 8 digit hexadecimal number

Here is my code...

// do not change this code except in the following ways:
//   * write code for the following functions:
//      * bigOrSmallEndian()
//      * getNextHexInt()
//      * printLinesForNumber()
//   * change studentName by changing "I. Forgot" to your actual name

#include <stdio.h>
#include <stdlib.h>

static char *studentName = "Tenzin Shakya";

// report whether machine is big or small endian
void bigOrSmallEndian()
{
    int num = 1;
    if(*(char *)&num == 1)
    {
      printf("\nbyte order: little-endian\n");
   }
   else
  {
      printf("\nbyte order: big-endian\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);

}






// print requested data for the given number
void printNumberData(int i)
{
}

// 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;
}

Edited 3 Years Ago by numbplum: Update

It needs to print out signBit, expBit, and fracBits.

What, pray tell, are expBit and fracBits supposed to represent for an integer?

Your code will interpret those 8 digits as an IEEE 754 32-bit floating point number and will print out information about that number. Do not worry about bad input. My test script does not do this.

Use scanf to get the user input. Your code should accept input with or without "0x" in front of it (scanf does this).

For each number, your code should print out:

the sign bit
the exponent bits, expressed as a decimal number
the fraction bits, expressed as an 8 digit hexadecimal number

This article has been dead for over six months. Start a new discussion instead.