Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

C-Kurs/ieee754/Musterlösung

< C-Kurs‎ | ieee754(Weitergeleitet von Ckurs/ieee754/Musterlösung)
/*
** Copyright (C) Sebastian Pipping <sebastian@pipping.org> and Katrin Lang <mail@katrinlang.de>
** Licensed under GPL v3 or later
**
** 2011-08-13 20:06 UTC+2
*/

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

typedef union _uint_float_t {
        unsigned int u;
        float f;
} uint_float_t;

static void dump_header() {
  printf("Index  31     24      16      8       0\n");
  printf("       |");
  signed int bit_index = sizeof(int) * 8 - 2;
  for (; bit_index >= 0; bit_index--) {
    putchar(bit_index % 8 ? '.' : '|');
  }
  putchar('\n');
}


static void dump_bits(float f) {
        uint_float_t store;
        store.f = f;
        printf("Data   ");
        signed int bit_index = sizeof(int) * 8 - 1;
        for (; bit_index >= 0; bit_index--) {
                const unsigned int mask = (unsigned int)1 << (bit_index);
                putchar((store.u & mask) ? '1' : '0');
        }
        printf("  %7.3ff\n", f);
}


int main(int argc, char ** argv) {
        assert(sizeof(unsigned int) == sizeof(float));

        dump_header();
        for (int i= 1; i<argc; i++){
          argv++;
          dump_bits(strtof(*argv, 0));
        }
        dump_header();

        return 0;
}