Compare commits
2 commits
ca14ee97e4
...
c723b55d05
| Author | SHA1 | Date | |
|---|---|---|---|
| c723b55d05 | |||
| 614fa462d8 |
2 changed files with 14 additions and 4 deletions
BIN
hallo
BIN
hallo
Binary file not shown.
18
hallo-welt.c
18
hallo-welt.c
|
|
@ -1,4 +1,5 @@
|
||||||
#include "hallo-welt.h"
|
#include "hallo-welt.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
@ -9,14 +10,22 @@
|
||||||
|
|
||||||
void hallo_welt(void) { printf(HALLO); }
|
void hallo_welt(void) { printf(HALLO); }
|
||||||
|
|
||||||
|
int zweierkomplement(int32_t i) { return ~i + 1; }
|
||||||
|
|
||||||
// use more then one int type
|
// use more then one int type
|
||||||
const char *binaerzahl(uint8_t bin) {
|
const char *binaerzahl(int32_t bin) {
|
||||||
char *str = malloc(sizeof(bin) * 8 +
|
char *str = malloc(sizeof(bin) * 8 +
|
||||||
1); // Allocate 8 bits for actual data and 1 Bit for the \0
|
1); // Allocate 8 bits for actual data and 1 Bit for the \0
|
||||||
// sign indicating the end of the string
|
// sign indicating the end of the string
|
||||||
|
bool is_negative;
|
||||||
if (!str)
|
if (!str)
|
||||||
return NULL;
|
return NULL;
|
||||||
uint8_t bb;
|
if (bin < 0) {
|
||||||
|
bin = zweierkomplement(bin);
|
||||||
|
is_negative = true;
|
||||||
|
}
|
||||||
|
int32_t bb; // that way we keep the memory lighter
|
||||||
|
printf(is_negative ? "-" : "");
|
||||||
for (int i = sizeof(bin) * 8 - 1; i >= 0; i--) {
|
for (int i = sizeof(bin) * 8 - 1; i >= 0; i--) {
|
||||||
bb = bin >> i;
|
bb = bin >> i;
|
||||||
str[i] = (bb % 2 == 1) ? 1 : 0;
|
str[i] = (bb % 2 == 1) ? 1 : 0;
|
||||||
|
|
@ -27,8 +36,9 @@ const char *binaerzahl(uint8_t bin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int getint(void) {
|
int getint(void) {
|
||||||
uint8_t i;
|
uint32_t i;
|
||||||
scanf("%hhu", &i);
|
printf("Geben Sie eine Dezimahlzahl ein: ");
|
||||||
|
scanf("%u", &i);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue