我正在做作业,但被卡住了。由于某些原因,我无法获得以下结果:

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


我曾尝试解决此问题,但我不知道哪里出了问题。以下代码是我对该项目的尝试。我觉得我快结束了,但我还没完。

这是我的代码:

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

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


此代码的结果是:

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


我相信问题出在printNumberData(),但我无法指出问题所在。

我希望有人能参与进来,因为我们将不胜感激。

最佳答案

因此,据我所知,唯一的问题是您的指数未签名。您可以通过将tru_exp更改为int8_t(或signed char)来解决此问题。或者通过简单地手动对数字进行符号扩展,例如if (tru_exp > 128) tru_exp -= 256;

关于c - 分析IEEE 754位模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14797418/

10-11 18:31