Compare commits
2 commits
main
...
placeholde
| Author | SHA1 | Date | |
|---|---|---|---|
| ad11ab7b5f | |||
| 62c0529e16 |
5 changed files with 18 additions and 57 deletions
|
|
@ -27,14 +27,12 @@ eine eigene Header Datei genutzt wird, ist hierbei egal.
|
|||
### 1b)
|
||||
|
||||
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.
|
||||
|
||||
## Aufgabe 2: Bitwise Operatoren
|
||||
|
||||
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:
|
||||
|
||||
|
|
@ -47,6 +45,3 @@ _Hinweis: Die negativen Zahlen werden im Zweierkomplement gespeichert. Es kann a
|
|||
|
||||
_Zur Erinnerung:_
|
||||
_Das Zweierkomplement wird gebildet, indem die Bits invertiert werden und anschließend 1 zur Zahl addiert wird._
|
||||
|
||||
_Hinweis:_
|
||||
_Überlauf._
|
||||
|
|
|
|||
BIN
hallo
BIN
hallo
Binary file not shown.
45
hallo-welt.c
45
hallo-welt.c
|
|
@ -1,45 +0,0 @@
|
|||
#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())); }
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#ifndef HALLO
|
||||
#define HALLO "Hallo Welt!"
|
||||
#else
|
||||
printf("HALLO ist schon definiert!");
|
||||
#endif
|
||||
18
hallo_welt.c
Normal file
18
hallo_welt.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
//TODO: Hier könnte etwas fehlen...
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int temp;
|
||||
printf("Bitte gib eine Dezimalzahl ein: ");
|
||||
scanf("%d", &temp);
|
||||
binaerzahl(temp); //TODO: ist bis jetzt noch nicht definiert
|
||||
}
|
||||
|
||||
/*
|
||||
* Die Funktion soll die eingegebene Zahl als Binärzahl ausgegeben werden
|
||||
* Hierfür sollen Bitwise Operatoren und/oder Bitwise Shift genutzt werden.
|
||||
*/
|
||||
void binaerzahl(int input){
|
||||
//TODO
|
||||
}
|
||||
Loading…
Reference in a new issue