Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:zahlensysteme:binary_solutions [2026-03-31 06:02] – [Tabelle] hof | gf_informatik:zahlensysteme:binary_solutions [2026-04-02 09:54] (aktuell) – [Umwandlung Dezimal-Binär] hof | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ## Binärzahl-Operationen mit Python | ## Binärzahl-Operationen mit Python | ||
| - | < | + | < |
| ### Umwandlung Binär-Dezimal | ### Umwandlung Binär-Dezimal | ||
| Nehmen wir `b = ' | Nehmen wir `b = ' | ||
| - | ^ Ziffer | + | ^ Position |
| - | ^ Position | + | ^ Ziffer |
| Um die Binärzahl in eine Dezimalzahl umzurechnen, | Um die Binärzahl in eine Dezimalzahl umzurechnen, | ||
| Zeile 12: | Zeile 11: | ||
| Zu jeder Ziffer gehört also die passende Zweierpotenz: | Zu jeder Ziffer gehört also die passende Zweierpotenz: | ||
| - | ^ Ziffer | + | ^ Position |
| - | ^ Position | + | ^ Ziffer |
| - | ^ Exponent | + | ^ Exponent |
| - | ^ Potenz | + | ^ Potenz |
| Die Schwierigkeit bei diesem Code ist, dass **Position und Exponent genau gegenteilig sind**: Die Position startet bei $0$ und zählt hoch, der Exponent startet bei $5$ und zählt herunter. | Die Schwierigkeit bei diesem Code ist, dass **Position und Exponent genau gegenteilig sind**: Die Position startet bei $0$ und zählt hoch, der Exponent startet bei $5$ und zählt herunter. | ||
| Zeile 23: | Zeile 22: | ||
| 1. **Zwei separate Variablen**: | 1. **Zwei separate Variablen**: | ||
| 2. **Binärstring umkehren**, also aus `' | 2. **Binärstring umkehren**, also aus `' | ||
| - | | + | |
| <nodisp 1> | <nodisp 1> | ||
| ++++Lösung| | ++++Lösung| | ||
| - | < | + | < |
| + | def binary_to_decimal(b): | ||
| """ | """ | ||
| d = 0 | d = 0 | ||
| + | exponent = len(b) - 1 | ||
| for digit in b: | for digit in b: | ||
| - | d = d*2 | ||
| if digit == ' | if digit == ' | ||
| - | d = d + 1 | + | d = d + 2**exponent |
| + | exponent = exponent - 1 | ||
| return d | return d | ||
| - | print(binary_to_decimal(" | + | print(binary_to_decimal(" |
| + | </ | ||
| ++++ | ++++ | ||
| </ | </ | ||
| - | |||
| ### Umwandlung Dezimal-Binär | ### Umwandlung Dezimal-Binär | ||
| Zeile 47: | Zeile 48: | ||
| <nodisp 1> | <nodisp 1> | ||
| ++++Lösung| | ++++Lösung| | ||
| - | < | + | < |
| def decimal_to_binary(d): | def decimal_to_binary(d): | ||
| """ | """ | ||
| Zeile 73: | Zeile 74: | ||
| <nodisp 2> | <nodisp 2> | ||
| ++++Lösung| | ++++Lösung| | ||
| - | < | + | < |
| def fill_zeros(b, | def fill_zeros(b, | ||
| while len(b) < digits: | while len(b) < digits: | ||
| Zeile 112: | Zeile 113: | ||
| <nodisp 2> | <nodisp 2> | ||
| ++++Lösung| | ++++Lösung| | ||
| - | <code python> | + | <html>< |
| def invert(b): | def invert(b): | ||
| """ | """ | ||
| Zeile 138: | Zeile 139: | ||
| result = result[-stellen: | result = result[-stellen: | ||
| return result | return result | ||
| - | </code> | + | </bottom-editor></ |
| ++++ | ++++ | ||
| </ | </ | ||
| Zeile 146: | Zeile 147: | ||
| Kopiere alle Funktionen oben in die gleiche Python-Datei. Überprüfe danach die Funktionsweise: | Kopiere alle Funktionen oben in die gleiche Python-Datei. Überprüfe danach die Funktionsweise: | ||
| - | <code python> | + | <html>< |
| a_dec = 42 | a_dec = 42 | ||
| b_dec = 19 | b_dec = 19 | ||
| Zeile 154: | Zeile 155: | ||
| difference_dec = binary_to_decimal(difference_bin) | difference_dec = binary_to_decimal(difference_bin) | ||
| print(f" | print(f" | ||
| - | </code> | + | </bottom-editor></ |
| Resultat: | Resultat: | ||