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 05:50] – [Binär-Addition] 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 = ' | ||
| - | ``` | + | ^ Position |
| - | Ziffer: | + | ^ Ziffer |
| - | Position: 012345 | + | |
| - | ``` | + | |
| Um die Binärzahl in eine Dezimalzahl umzurechnen, | Um die Binärzahl in eine Dezimalzahl umzurechnen, | ||
| $$1 \cdot 2^\color{red}{5} + 0 \cdot 2^\color{red}{4} + 1 \cdot 2^\color{red}{3} + 0 \cdot 2^\color{red}{2} + 1 \cdot 2^\color{red}{1} + 0 \cdot 2^\color{red}{0}$$ | $$1 \cdot 2^\color{red}{5} + 0 \cdot 2^\color{red}{4} + 1 \cdot 2^\color{red}{3} + 0 \cdot 2^\color{red}{2} + 1 \cdot 2^\color{red}{1} + 0 \cdot 2^\color{red}{0}$$ | ||
| - | Zu jeder Ziffer gehört also die passende | + | Zu jeder Ziffer gehört also die passende |
| - | ``` | + | ^ Position |
| - | Ziffer: | + | ^ Ziffer |
| - | Position: 012345 | + | ^ 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 24: | Zeile 21: | ||
| 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(" |
| + | </ | ||
| ++++ | ++++ | ||
| </ | </ | ||
| Zeile 48: | Zeile 48: | ||
| <nodisp 1> | <nodisp 1> | ||
| ++++Lösung| | ++++Lösung| | ||
| - | < | + | < |
| def decimal_to_binary(d): | def decimal_to_binary(d): | ||
| """ | """ | ||
| Zeile 62: | Zeile 62: | ||
| ++++ | ++++ | ||
| </ | </ | ||
| + | |||
| ### Binär-Addition | ### Binär-Addition | ||
| Umsetzungsidee: | Umsetzungsidee: | ||
| Zeile 67: | Zeile 68: | ||
| * An jeder Position das Bit aus `a` und `b` sowie einen allfälligen Übertrag addieren. | * An jeder Position das Bit aus `a` und `b` sowie einen allfälligen Übertrag addieren. | ||
| * Das Resultat-Bit an der Position ist `1`, wenn die Summe `1` oder `3` ist, sonst `0`. | * Das Resultat-Bit an der Position ist `1`, wenn die Summe `1` oder `3` ist, sonst `0`. | ||
| - | * Tipp: also genau dem Resultat von `summe % 2`... | + | * Tipp: also genau das Resultat von `summe % 2`... |
| * Der Übertrag auf die nächste Stelle ist `1` wenn die Summe `2` oder `3` ist. | * Der Übertrag auf die nächste Stelle ist `1` wenn die Summe `2` oder `3` ist. | ||
| * Also genau dem Resultat von ...? | * Also genau dem Resultat von ...? | ||
| 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 106: | Zeile 107: | ||
| ++++ | ++++ | ||
| </ | </ | ||
| - | > | + | |
| ### Binär-Subtraktion | ### Binär-Subtraktion | ||
| - | <code python> | + | <nodisp 2> |
| + | ++++Lösung| | ||
| + | < | ||
| def invert(b): | def invert(b): | ||
| """ | """ | ||
| Zeile 135: | Zeile 139: | ||
| result = result[-stellen: | result = result[-stellen: | ||
| return result | return result | ||
| - | </code> | + | </bottom-editor></ |
| + | ++++ | ||
| + | </nodisp> | ||
| ### Ausprobieren | ### Ausprobieren | ||
| Zeile 141: | 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 149: | 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: | ||