Compare commits

..

2 commits

Author SHA1 Message Date
ad11ab7b5f readme 2026-06-08 23:30:51 +02:00
62c0529e16 placeholder version 2026-06-08 23:28:36 +02:00
5 changed files with 18 additions and 57 deletions

View file

@ -27,14 +27,12 @@ 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:
@ -47,6 +45,3 @@ _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

Binary file not shown.

View file

@ -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())); }

View file

@ -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
View 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
}