Compare commits
6 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 716cb8d507 | |||
| c723b55d05 | |||
| 614fa462d8 | |||
| ca14ee97e4 | |||
| cad5b1e656 | |||
| 17e7f01fdf |
5 changed files with 57 additions and 61 deletions
|
|
@ -27,12 +27,14 @@ eine eigene Header Datei genutzt wird, ist hierbei egal.
|
||||||
### 1b)
|
### 1b)
|
||||||
|
|
||||||
Was passiert, wenn die Zahl größer als 255 ist?
|
Was passiert, wenn die Zahl größer als 255 ist?
|
||||||
|
-> Überlauf.
|
||||||
|
|
||||||
Passe die Funktion so an, dass auch größere Zahlen (0 bis 232-1) als Binärzahlen ausgegeben werden können.
|
Passe die Funktion so an, dass auch größere Zahlen (0 bis 232-1) als Binärzahlen ausgegeben werden können.
|
||||||
|
|
||||||
## Aufgabe 2: Bitwise Operatoren
|
## Aufgabe 2: Bitwise Operatoren
|
||||||
|
|
||||||
Was passiert, wenn die Zahl negativ ist?
|
Was passiert, wenn die Zahl negativ ist?
|
||||||
|
-> bevor Aufgabe 2, immernoch ein Überlauf.
|
||||||
|
|
||||||
Erweitere die Funktion erneut, sodass auch negative Zahlen bearbeitet werden. Hierbei soll bei der Ausgabe erst ein Minus stehen und dahinter die positive Binärzahl. Beispiel:
|
Erweitere die Funktion erneut, sodass auch negative Zahlen bearbeitet werden. Hierbei soll bei der Ausgabe erst ein Minus stehen und dahinter die positive Binärzahl. Beispiel:
|
||||||
|
|
||||||
|
|
@ -45,3 +47,6 @@ _Hinweis: Die negativen Zahlen werden im Zweierkomplement gespeichert. Es kann a
|
||||||
|
|
||||||
_Zur Erinnerung:_
|
_Zur Erinnerung:_
|
||||||
_Das Zweierkomplement wird gebildet, indem die Bits invertiert werden und anschließend 1 zur Zahl addiert wird._
|
_Das Zweierkomplement wird gebildet, indem die Bits invertiert werden und anschließend 1 zur Zahl addiert wird._
|
||||||
|
|
||||||
|
_Hinweis:_
|
||||||
|
_Überlauf._
|
||||||
|
|
|
||||||
BIN
hallo
Executable file
BIN
hallo
Executable file
Binary file not shown.
45
hallo-welt.c
Normal file
45
hallo-welt.c
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "hallo-welt.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifndef HALLO
|
||||||
|
#define HALLO "Hallo Welt"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void hallo_welt(void) { printf(HALLO); }
|
||||||
|
|
||||||
|
int zweierkomplement(int32_t i) { return ~i + 1; }
|
||||||
|
|
||||||
|
// use more then one int type
|
||||||
|
const char *binaerzahl(int32_t bin) {
|
||||||
|
char *str = malloc(sizeof(bin) * 8 +
|
||||||
|
1); // Allocate 8 bits for actual data and 1 Bit for the \0
|
||||||
|
// sign indicating the end of the string
|
||||||
|
bool is_negative;
|
||||||
|
if (!str)
|
||||||
|
return NULL;
|
||||||
|
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--) {
|
||||||
|
bb = bin >> i;
|
||||||
|
str[i] = (bb % 2 == 1) ? 1 : 0;
|
||||||
|
printf("%d", str[i]);
|
||||||
|
}
|
||||||
|
str[sizeof(bin)] = '\0';
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getint(void) {
|
||||||
|
uint32_t i;
|
||||||
|
printf("Geben Sie eine Dezimahlzahl ein: ");
|
||||||
|
scanf("%u", &i);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) { printf("%s", binaerzahl(getint())); }
|
||||||
7
hallo-welt.h
Normal file
7
hallo-welt.h
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifndef HALLO
|
||||||
|
#define HALLO "Hallo Welt!"
|
||||||
|
#else
|
||||||
|
printf("HALLO ist schon definiert!");
|
||||||
|
#endif
|
||||||
61
hallo_welt.c
61
hallo_welt.c
|
|
@ -1,61 +0,0 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void binaerzahl1A(int input);
|
|
||||||
void binaerzahl1B(int input);
|
|
||||||
void binaerzahl2(int input);
|
|
||||||
int32_t zweierkomplement(int32_t input);
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
int temp;
|
|
||||||
printf("Bitte gib eine Dezimalzahl ein: ");
|
|
||||||
scanf("%d", &temp);
|
|
||||||
binaerzahl2(
|
|
||||||
temp); // Hier muss der Funktionsaufruf entsprechend angepasst werden
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Musterlösung 1 a)
|
|
||||||
*/
|
|
||||||
void binaerzahl1A(int input) {
|
|
||||||
for (int8_t i = 0; i < 8; i++) { // Wir geben 8 mal aus:
|
|
||||||
uint8_t erg =
|
|
||||||
(input
|
|
||||||
<< i); // Wir verschieben so, dass das aktuelle Bit am Anfang steht
|
|
||||||
erg = erg >> 7; // Da wir uint8_t nutzen, wird der Rest abgeschnitten und
|
|
||||||
// wir verschieben zurück, sodass wir nur 0 oder 1 ausgeben
|
|
||||||
printf("%d", erg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Musterlösung 1 b)
|
|
||||||
*/
|
|
||||||
void binaerzahl1B(int input) {
|
|
||||||
for (uint8_t i = 0; i < 32; i++) {
|
|
||||||
uint32_t erg = (input << i);
|
|
||||||
erg = erg >> 31;
|
|
||||||
printf("%d", erg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Musterlösung 2
|
|
||||||
*/
|
|
||||||
void binaerzahl2(int input) {
|
|
||||||
if (input < 0) {
|
|
||||||
input = zweierkomplement(input);
|
|
||||||
printf("-");
|
|
||||||
}
|
|
||||||
for (uint8_t i = 0; i < 32; i++) {
|
|
||||||
uint32_t erg = (input << i);
|
|
||||||
erg = erg >> 31;
|
|
||||||
printf("%d", erg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t zweierkomplement(int32_t input) {
|
|
||||||
input = ~input;
|
|
||||||
input++;
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue