C-Kurs/ieee754/Musterlösung: Unterschied zwischen den Versionen
Nighoo (Diskussion | Beiträge) (lösung statt vorgabe) |
PaulG (Diskussion | Beiträge) K (verschob „Ckurs/ieee754/Musterlösung“ nach „C-Kurs/ieee754/Musterlösung“) |
||
| (Eine dazwischenliegende Version von einem anderen Benutzer wird nicht angezeigt) | |||
| Zeile 27: | Zeile 27: | ||
| − | static void | + | static void dump_bits(float f) { |
| − | dump_bits(float f) { | ||
uint_float_t store; | uint_float_t store; | ||
store.f = f; | store.f = f; | ||
printf("Data "); | printf("Data "); | ||
| − | + | signed int bit_index = sizeof(int) * 8 - 1; | |
for (; bit_index >= 0; bit_index--) { | for (; bit_index >= 0; bit_index--) { | ||
| − | const unsigned int mask = (unsigned int)1 << (bit_index | + | const unsigned int mask = (unsigned int)1 << (bit_index); |
putchar((store.u & mask) ? '1' : '0'); | putchar((store.u & mask) ? '1' : '0'); | ||
} | } | ||
| Zeile 45: | Zeile 44: | ||
dump_header(); | dump_header(); | ||
| − | + | for (int i= 1; i<argc; i++){ | |
| − | + | argv++; | |
| − | + | dump_bits(strtof(*argv, 0)); | |
| − | + | } | |
dump_header(); | dump_header(); | ||
Aktuelle Version vom 5. März 2013, 17:37 Uhr
/*
** 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;
}