Compare commits
No commits in common. "b36a69ed4145d5252f0807838a137ef142827c4b" and "d1aba291b9e5a337b84e117abc92713fc058ebe2" have entirely different histories.
b36a69ed41
...
d1aba291b9
40
dualwand.c
40
dualwand.c
@ -1,40 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void umrechnen(int number);
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
int number = 0;
|
|
||||||
int validInput = 0;
|
|
||||||
|
|
||||||
while (!validInput) {
|
|
||||||
printf("Bitte geben Sie eine Zahl ein: ");
|
|
||||||
if (scanf("%d", &number) != 1) {
|
|
||||||
printf("Ungueltige Eingabe. Bitte geben Sie eine gueltige Zahl ein.\n");
|
|
||||||
while (getchar() != '\n');
|
|
||||||
} else {
|
|
||||||
validInput = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
umrechnen(number);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void umrechnen(int number) {
|
|
||||||
int bits = sizeof(int) * 8; // Anzahl der Bits in einem int (z. B. 32 Bits)
|
|
||||||
unsigned int mask = 1 << (bits - 1); // Maske für das höchstwertige Bit
|
|
||||||
|
|
||||||
// Wenn die Zahl negativ ist, wird sie als Zweierkomplement interpretiert
|
|
||||||
unsigned int value = number;
|
|
||||||
|
|
||||||
for (int i = 0; i < bits; i++) {
|
|
||||||
// Prüfe das höchstwertige Bit und schiebe die Maske nach rechts
|
|
||||||
if (value & mask) {
|
|
||||||
printf("1");
|
|
||||||
} else {
|
|
||||||
printf("0");
|
|
||||||
}
|
|
||||||
mask >>= 1; // Schiebe die Maske um ein Bit nach rechts
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user