Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
gf_informatik:zahlensysteme [2023-05-31 09:20] – [Lösungen] scagf_informatik:zahlensysteme [2025-06-24 06:59] (aktuell) – [Lösungen] sca
Zeile 1: Zeile 1:
 ====== Zahlensysteme ====== ====== Zahlensysteme ======
  
-Dossier: {{ :gf_informatik:gfif_zahlensysteme_dossier.pdf |}}+**{{ :gf_informatik:gfif_zahlensysteme_dossier.pdf |Dossier Zahlensysteme}}**
  
 ++++ Lernziele| ++++ Lernziele|
Zeile 63: Zeile 63:
 Dementsprechend gilt also $-42_{10} = 1101\,0110$, *falls* wir $8-$Bit Zahlen inkl. negative Zahlen betrachten. Ohne diese Information würde man die Zahl wohl als die positive Zahl $214_{10}$ interpretieren. Man muss also immer ganz klar sagen, *wie man eine jeweilige Zahl zu interpretieren hat!* Dementsprechend gilt also $-42_{10} = 1101\,0110$, *falls* wir $8-$Bit Zahlen inkl. negative Zahlen betrachten. Ohne diese Information würde man die Zahl wohl als die positive Zahl $214_{10}$ interpretieren. Man muss also immer ganz klar sagen, *wie man eine jeweilige Zahl zu interpretieren hat!*
  
-=== 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 = '100101'` als Beispiel. Jede Ziffer in diesem String steht an einer bestimmten Position: 
 +``` 
 +Ziffer:   100101 
 +Position: 012345 
 +``` 
 +Um die Binärzahl in eine Dezimalzahl umzurechnen, müssen wir potenzieren: 
 +$$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:   100101 
 +Position: 012345 
 +Potenz:   543210 
 +``` 
 +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**: Eine für die Position und eine für den Exponenten. 
 +   2. Man **dreht den Binärstring um**, also aus `'100101'` wird `'101001'`: Jetzt stimmen Position und Exponent überein. Allerdings haben  wir nie besprochen, wie man diese Umkrehrung einfach erreichen kann. 
 + 
 +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): 
-    = 0 # decimal nr +    """ 
-    0 +    Löse mit zwei Variablen: 
-    while len(b): +    - Variable i (der for-Schleife) zählt alle Positionen durch 
-        d = d + int(b[len(b)-i-1]) * 2**i # len(b)-i-1 to go through b in reverse order +    - Variable exponent legt Exponenten fest und zählt herunter 
-        i + +    """ 
-    return d+    dec = 0 # Dezimalzahl 
 +    exponent len(b) - 1 # Exponent Startwert 
 +    for in range(len(b)): # Schleife über Position 
 +        if b[i] == '1': 
 +            dec = dec + 2**exponent 
 +        exponent exponent - 
 +    return dec
  
-def binary_to_decimal_2(b): +print(binary_to_decimal_1a('100101')) 
-    d = 0 # decimal nr + 
-    = 0 +def binary_to_decimal_1b(b): 
-    while i < len(b): +    """ 
-        d + int(b[-i-1]) * 2**i # can also use negative indices to go through d in reverse order +    Wie Varianbte 1a, nur gehen wir mit for-Schleife direkt die Elemente des Binärstrings durch (ohne Position)  
-        i + +    """ 
-    return d+    dec = 0 # Dezimalzahl 
 +    exponent = len(b) - 1 # Exponent Startwert 
 +    for nr in b# Schleife über Position 
 +        if nr == '1': 
 +            dec = dec + 2**exponent 
 +        exponent exponent - 
 +    return dec
  
-def binary_to_decimal_3(b): +print(binary_to_decimal_1b('100101')) 
-    = 0 # decimal nr + 
-    = len(b) - 1 +def binary_to_decimal_1c(b): 
-    power = 1 +    """ 
-    while >= 0: +    Wie Varianbte 1b, nur ohne if. Dafür wandeln wir die Elemente des Binärstrings in eine Zahl um. 
-        d + int(b[i]* power +    """ 
-        power = power * 2 +    dec = 0 # Dezimalzahl 
-        i = i - +    exponent = len(b) - 1 # Exponent Startwert 
-    return d+    for nr in b: # Schleife über Position 
 +        dec dec + int(nr) * 2**exponent 
 +        exponent = exponent - 
 +    return dec 
 + 
 +print(binary_to_decimal_1c('100101')) 
 + 
 +def binary_to_decimal_1d(b): 
 +    """ 
 +    Wie Varianbte 1a, nur wird exponent direkt aus 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 
 +        exponent len(b) - i - 1 # Berechnung Exponent 
 +        if b[i== '1': 
 +            dec = dec + 2**exponent 
 +    return dec 
 + 
 +print(binary_to_decimal_1d('100101'))
 </code> </code>
 +
 ++++ ++++
  
Zeile 150: Zeile 201:
  
 <code python> <code python>
 +
 def binary_add(b1,b2): def binary_add(b1,b2):
 +    b1 = b1.replace(" ","") # remove all blanks
 +    b2 = b2.replace(" ","") # remove all blanks
     # 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 = '1' + summe         summe = '1' + summe
     return summe     return summe
 + 
 def invert(b): def invert(b):
 +    b = b.replace(" ","") # remove all blanks
     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(" ","") # remove all blanks
     b = invert(b)     b = invert(b)
     b = binary_add(b,'1')     b = binary_add(b,'1')
     return b     return b
 + 
 def binary_sub(b1,b2): # calc b1-b2 def binary_sub(b1,b2): # calc b1-b2
 +    b1 = b1.replace(" ","") # remove all blanks
 +    b2 = b2.replace(" ","") # remove all blanks    
     # 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 = '0' + b2         b2 = '0' + 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 = '0' + b1     b1 = '0' + b1
     b2 = '0' + b2     b2 = '0' + b2
-    + 
     result = binary_add(b1,complement(b2))     result = binary_add(b1,complement(b2))
     result = result[1:] # remove first bit     result = result[1:] # remove first bit
          
-    while result[0] == '0': # remove all zeros at beginning (optional)+    while len(result) > 1 and result[0] == '0': # remove all zeros at beginning (optional)
         result = result[1:]         result = result[1:]
 +    return result
 +
 +def binary_mul(b1,b2):
 +    b1 = b1.replace(" ","") # remove all blanks
 +    b2 = b2.replace(" ","") # remove all blanks    
 +    result = ""
 +    
 +    i = len(b1) - 1
 +    while i >= 0:
 +        if b1[i] == '1':
 +            result = binary_add(result,b2)
 +        b2 = b2 + '0'
 +        i = i - 1
 +    
 +    while len(result) > 1 and result[0] == '0': # remove all zeros at beginning
 +        result = result[1:]
 +
     return result     return result
 </code> </code>
Zeile 216: Zeile 291:
 ++++ ++++
  
-</nodisp> 
  
  
Zeile 331: Zeile 405:
 </code> </code>
 ++++ ++++
 +
 +++++Function Testing|
 +
 +<code python>
 +### FUNCTION TESTING CODE
 +for i in range(1000):
 +    da = random.randint(0,165)
 +    db = random.randint(0,165)
 +    if db > da: da,db = db,da
 +    a = bin(da)[2:]
 +    b = bin(db)[2:]
 +    if not a == invert(invert(a)):
 +        print("ERROR Invert: ",a,invert(a))
 +    
 +    if not binary_add(a,b) == bin(int(a,2)+int(b,2))[2:]:
 +        print("ERROR Add: ",a,b)
 +    
 +    if not binary_sub(a,b) == bin(int(a,2)-int(b,2))[2:]:
 +        print("ERROR Sub: ",a,b)
 +</code>
 +++++
 +
 +++++Exercise Generator|
 +<code python>
 +### EXERCISE GENERATOR
 +for i in range(3):
 +    da = random.randint(128,256)
 +    db = random.randint(128,256)
 +    if db > da: da,db = db,da
 +    a = bin(da)[2:]
 +    b = bin(db)[2:   
 +    print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein:\n" + a + " + " + b )
 +    print("LOESUNG: " + binary_add(a,b) + "\n")
 +
 +for i in range(3):
 +    da = random.randint(128,256)
 +    a = bin(da)[2:]
 +    print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein. 2-er Komplement in 8-Bit von:\n" + a)
 +    print("LOESUNG: " + binary_complement(a) + "\n")
 +
 +for i in range(3):
 +    da = random.randint(128,256)
 +    db = random.randint(128,256)
 +    if db > da: da,db = db,da
 +    a = bin(da)[2:]
 +    b = bin(db)[2:   
 +    print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein. Lasse überflüssige Nullen auf linker Seite weg.\n" + a + " - " + b )
 +    print("LOESUNG: " + binary_sub(a,b) + "\n")
 +
 +for i in range(3):
 +    da = random.randint(1,15)
 +    db = random.randint(1,15)
 +    if db > da: da,db = db,da
 +    a = bin(da)[2:]
 +    b = bin(db)[2:]
 +    while len(a) < 4: a = '0' + a  
 +    while len(b) < 4: b = '0' + b  
 +    print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein.\n" + a + " * " + b )
 +    print("LOESUNG: " + bin(da*db)[2:] + "\n")
 +</code>
 +++++
 +
 +
 </nodisp> </nodisp>
  
  • gf_informatik/zahlensysteme.1685524811.txt.gz
  • Zuletzt geändert: 2023-05-31 09:20
  • von sca