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 [2023-05-31 09:20] – [Lösungen] sca | gf_informatik:zahlensysteme [2025-06-24 06:59] (aktuell) – [Lösungen] sca | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== Zahlensysteme ====== | ====== Zahlensysteme ====== | ||
| - | Dossier: | + | **{{ : |
| ++++ Lernziele| | ++++ Lernziele| | ||
| Zeile 63: | Zeile 63: | ||
| Dementsprechend gilt also $-42_{10} = 1101\, | Dementsprechend gilt also $-42_{10} = 1101\, | ||
| - | === Subraktion | + | === Subtraktion |
| **Beispiel 1:** Wir wollen in $8$-Bit die folgende Subtraktion durchführen: | **Beispiel 1:** Wir wollen in $8$-Bit die folgende Subtraktion durchführen: | ||
| Zeile 99: | Zeile 99: | ||
| ===== Lösungen ===== | ===== Lösungen ===== | ||
| - | |||
| - | <nodisp 1> | ||
| ++++Binary to Decimal| | ++++Binary to Decimal| | ||
| - | Verschiedene Versionen: | + | Nehmen wir `b = ' |
| + | ``` | ||
| + | Ziffer: | ||
| + | Position: 012345 | ||
| + | ``` | ||
| + | Um die Binärzahl in eine Dezimalzahl umzurechnen, | ||
| + | $$1 \cdot 2^\color{red}{5} + 0 \cdot 2^\color{red}{4} + 0 \cdot 2^\color{red}{3} + 1 \cdot 2^\color{red}{2} + 0 \cdot 2^\color{red}{1} + 1 \cdot 2^\color{red}{0}$$ | ||
| + | |||
| + | Zu jeder Ziffer gehört also die passende Potenz: | ||
| + | ``` | ||
| + | Ziffer: | ||
| + | Position: 012345 | ||
| + | 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. Man macht **zwei separate Variablen**: | ||
| + | 2. Man **dreht den Binärstring um**, also aus `' | ||
| + | |||
| + | Hier einige mögliche Lösungen für den ersten Ansatz: | ||
| <code python> | <code python> | ||
| - | def binary_to_decimal_1(b): | + | def binary_to_decimal_1a(b): |
| - | | + | |
| - | | + | Löse mit zwei Variablen: |
| - | | + | - Variable i (der for-Schleife) zählt alle Positionen durch |
| - | | + | - Variable exponent legt Exponenten fest und zählt herunter |
| - | | + | """ |
| - | return | + | dec = 0 # Dezimalzahl |
| + | | ||
| + | | ||
| + | | ||
| + | dec = dec + 2**exponent | ||
| + | | ||
| + | return | ||
| - | def binary_to_decimal_2(b): | + | print(binary_to_decimal_1a(' |
| - | | + | |
| - | | + | def binary_to_decimal_1b(b): |
| - | | + | |
| - | | + | |
| - | | + | """ |
| - | return | + | dec = 0 # Dezimalzahl |
| + | | ||
| + | for nr in b: # Schleife über Position | ||
| + | | ||
| + | dec = dec + 2**exponent | ||
| + | | ||
| + | return | ||
| - | def binary_to_decimal_3(b): | + | print(binary_to_decimal_1b(' |
| - | | + | |
| - | | + | def binary_to_decimal_1c(b): |
| - | | + | |
| - | | + | Wie Varianbte 1b, nur ohne if. Dafür wandeln wir die Elemente des Binärstrings in eine Zahl um. |
| - | | + | """ |
| - | power = power * 2 | + | dec = 0 # Dezimalzahl |
| - | i = i - 1 | + | |
| - | return | + | |
| + | dec = dec + int(nr) * 2**exponent | ||
| + | exponent = exponent - 1 | ||
| + | | ||
| + | |||
| + | print(binary_to_decimal_1c(' | ||
| + | |||
| + | def binary_to_decimal_1d(b): | ||
| + | """ | ||
| + | Wie Varianbte 1a, nur wird exponent direkt aus i berechnet und muss dadurch nicht in jedem Schritt der Schleife um 1 reduziert werden. | ||
| + | """ | ||
| + | dec = 0 # Dezimalzahl | ||
| + | for i in range(len(b)): # Schleife über Position | ||
| + | | ||
| + | | ||
| + | dec = dec + 2**exponent | ||
| + | return | ||
| + | |||
| + | print(binary_to_decimal_1d(' | ||
| </ | </ | ||
| + | |||
| ++++ | ++++ | ||
| Zeile 150: | Zeile 201: | ||
| <code python> | <code python> | ||
| + | |||
| def binary_add(b1, | def binary_add(b1, | ||
| + | b1 = b1.replace(" | ||
| + | b2 = b2.replace(" | ||
| # make sure strings have same length | # make sure strings have same length | ||
| while len(b1) < len(b2): | while len(b1) < len(b2): | ||
| Zeile 178: | Zeile 232: | ||
| summe = ' | summe = ' | ||
| return summe | return summe | ||
| + | |||
| def invert(b): | def invert(b): | ||
| + | b = b.replace(" | ||
| inv = "" | inv = "" | ||
| i = 0 | i = 0 | ||
| Zeile 189: | Zeile 244: | ||
| i = i + 1 | i = i + 1 | ||
| return inv | return inv | ||
| + | |||
| def complement(b): | def complement(b): | ||
| + | b = b.replace(" | ||
| b = invert(b) | b = invert(b) | ||
| b = binary_add(b,' | b = binary_add(b,' | ||
| return b | return b | ||
| + | |||
| def binary_sub(b1, | def binary_sub(b1, | ||
| + | b1 = b1.replace(" | ||
| + | b2 = b2.replace(" | ||
| # make sure strings have same length | # make sure strings have same length | ||
| while len(b1) < len(b2): | while len(b1) < len(b2): | ||
| Zeile 201: | Zeile 259: | ||
| while len(b1) > len(b2): | while len(b1) > len(b2): | ||
| b2 = ' | b2 = ' | ||
| - | | + | |
| # add additional zero at left (to distinguish between positive and negative numbers) | # add additional zero at left (to distinguish between positive and negative numbers) | ||
| b1 = ' | b1 = ' | ||
| b2 = ' | b2 = ' | ||
| - | | + | |
| result = binary_add(b1, | result = binary_add(b1, | ||
| result = result[1:] # remove first bit | result = result[1:] # remove first bit | ||
| | | ||
| - | while result[0] == ' | + | while len(result) > 1 and result[0] == ' |
| result = result[1:] | result = result[1:] | ||
| + | return result | ||
| + | |||
| + | def binary_mul(b1, | ||
| + | b1 = b1.replace(" | ||
| + | b2 = b2.replace(" | ||
| + | result = "" | ||
| + | | ||
| + | i = len(b1) - 1 | ||
| + | while i >= 0: | ||
| + | if b1[i] == ' | ||
| + | result = binary_add(result, | ||
| + | b2 = b2 + ' | ||
| + | i = i - 1 | ||
| + | | ||
| + | while len(result) > 1 and result[0] == ' | ||
| + | result = result[1:] | ||
| + | |||
| return result | return result | ||
| </ | </ | ||
| Zeile 216: | Zeile 291: | ||
| ++++ | ++++ | ||
| - | </ | ||
| Zeile 331: | Zeile 405: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| + | |||
| + | ++++Function Testing| | ||
| + | |||
| + | <code python> | ||
| + | ### FUNCTION TESTING CODE | ||
| + | for i in range(1000): | ||
| + | da = random.randint(0, | ||
| + | db = random.randint(0, | ||
| + | if db > da: da,db = db,da | ||
| + | a = bin(da)[2:] | ||
| + | b = bin(db)[2:] | ||
| + | if not a == invert(invert(a)): | ||
| + | print(" | ||
| + | | ||
| + | if not binary_add(a, | ||
| + | print(" | ||
| + | | ||
| + | if not binary_sub(a, | ||
| + | print(" | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++Exercise Generator| | ||
| + | <code python> | ||
| + | ### EXERCISE GENERATOR | ||
| + | for i in range(3): | ||
| + | da = random.randint(128, | ||
| + | db = random.randint(128, | ||
| + | if db > da: da,db = db,da | ||
| + | a = bin(da)[2:] | ||
| + | b = bin(db)[2: | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | for i in range(3): | ||
| + | da = random.randint(128, | ||
| + | a = bin(da)[2:] | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | for i in range(3): | ||
| + | da = random.randint(128, | ||
| + | db = random.randint(128, | ||
| + | if db > da: da,db = db,da | ||
| + | a = bin(da)[2:] | ||
| + | b = bin(db)[2: | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | for i in range(3): | ||
| + | da = random.randint(1, | ||
| + | db = random.randint(1, | ||
| + | if db > da: da,db = db,da | ||
| + | a = bin(da)[2:] | ||
| + | b = bin(db)[2:] | ||
| + | while len(a) < 4: a = ' | ||
| + | while len(b) < 4: b = ' | ||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| </ | </ | ||