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 [2025-05-17 08:11] – [Binär-Subtraktion] 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 | ||
| + | Nehmen wir `b = ' | ||
| + | ^ Position | ||
| + | ^ Ziffer | ||
| - | ### Umwandlung Binär-Dezimal | + | 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}$$ | ||
| + | |||
| + | Zu jeder Ziffer gehört also die passende Zweierpotenz: | ||
| + | ^ Position | ||
| + | ^ Ziffer | ||
| + | ^ Exponent | ||
| + | ^ 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. | ||
| + | |||
| + | Dieses Problem kann man unterschiedlich lösen. Zwei mögliche Ansätze sind: | ||
| + | |||
| + | 1. **Zwei separate Variablen**: | ||
| + | 2. **Binärstring umkehren**, also aus `' | ||
| + | 3. Das Zwischenresultat wird **fortlaufend mit 2 multipliziert**. | ||
| + | |||
| + | <nodisp 1> | ||
| + | ++++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 | + | |
| - | | + | |
| + | | ||
| return d | return d | ||
| - | print(binary_to_decimal(" | + | print(binary_to_decimal(" |
| + | </ | ||
| + | ++++ | ||
| + | </ | ||
| ### Umwandlung Dezimal-Binär | ### Umwandlung Dezimal-Binär | ||
| - | Restwertalgorithmus: | + | Der {{gf_informatik: |
| - | < | + | |
| + | <nodisp 1> | ||
| + | ++++Lösung| | ||
| + | < | ||
| + | def decimal_to_binary(d): | ||
| """ | """ | ||
| b = "" | b = "" | ||
| while d > 0: | while d > 0: | ||
| - | | + | |
| d = d // 2 | d = d // 2 | ||
| - | b = str(r) + b | + | b = str(rest) + b |
| return b | return b | ||
| - | print(decimal_to_binary(42))</ | + | print(decimal_to_binary(42)) |
| + | </ | ||
| + | ++++ | ||
| + | </nodisp> | ||
| ### Binär-Addition | ### Binär-Addition | ||
| - | < | + | Umsetzungsidee: |
| + | * Beide Strings paarweise [[gf_informatik: | ||
| + | * 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`. | ||
| + | * Tipp: also genau das Resultat von `summe % 2`... | ||
| + | * Der Übertrag auf die nächste Stelle ist `1` wenn die Summe `2` oder `3` ist. | ||
| + | * Also genau dem Resultat von ...? | ||
| + | |||
| + | <nodisp 2> | ||
| + | ++++Lösung| | ||
| + | < | ||
| + | def fill_zeros(b, | ||
| while len(b) < digits: | while len(b) < digits: | ||
| b = " | b = " | ||
| return b | return b | ||
| - | def binary_add(a, | + | |
| + | def binary_add(a, | ||
| """ | """ | ||
| n = max(len(a), len(b)) | n = max(len(a), len(b)) | ||
| Zeile 41: | Zeile 87: | ||
| | | ||
| out = "" | out = "" | ||
| - | carry = 0 | + | carry = 0 # Übertrag |
| index = n - 1 | index = n - 1 | ||
| while index >= 0: | while index >= 0: | ||
| Zeile 47: | Zeile 93: | ||
| digit_b = int(b[index]) | digit_b = int(b[index]) | ||
| sum = digit_a + digit_b + carry | sum = digit_a + digit_b + carry | ||
| - | | + | |
| - | out = " | + | carry = sum // 2 |
| - | carry = 0 | + | out = str(bit) |
| - | | + | |
| - | out = " | + | |
| - | | + | |
| - | elif sum == 2: | + | |
| - | out = " | + | |
| - | carry = 1 | + | |
| - | | + | |
| - | | + | |
| - | carry = 1 | + | |
| index = index - 1 | index = index - 1 | ||
| + | | ||
| if carry != 0: | if carry != 0: | ||
| out = str(carry) + out | out = str(carry) + out | ||
| return out | return out | ||
| - | print(binary_add(" | + | print(binary_add(" |
| + | </ | ||
| + | ++++ | ||
| + | </ | ||
| + | |||
| ### Binär-Subtraktion | ### Binär-Subtraktion | ||
| - | <code python> | + | <nodisp 2> |
| + | ++++Lösung| | ||
| + | < | ||
| def invert(b): | def invert(b): | ||
| """ | """ | ||
| Zeile 93: | Zeile 139: | ||
| result = result[-stellen: | result = result[-stellen: | ||
| return result | return result | ||
| - | </code> | + | </bottom-editor></ |
| + | ++++ | ||
| + | </ | ||
| ### Ausprobieren | ### Ausprobieren | ||
| Zeile 100: | 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 | ||
| a_bin = decimal_to_binary(a_dec) | a_bin = decimal_to_binary(a_dec) | ||
| b_bin = decimal_to_binary(b_dec) | b_bin = decimal_to_binary(b_dec) | ||
| - | difference | + | difference_bin |
| - | print(f" | + | difference_dec = binary_to_decimal(difference_bin) |
| - | </code> | + | print(f" |
| + | </bottom-editor></ | ||
| Resultat: | Resultat: | ||