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-27 12:01] – [Subraktion] scagf_informatik:zahlensysteme [2023-06-06 21:30] (aktuell) – [Lösungen] sca
Zeile 25: Zeile 25:
    * binär multiplizieren    * binär multiplizieren
    * direkt Umrechnen binär <-> hexadezimal    * direkt Umrechnen binär <-> hexadezimal
 +   * umrechnen: hexadezimal <-> dezimal
  
 **Code:** **Code:**
Zeile 64: Zeile 65:
 === Subraktion === === Subraktion ===
  
-Beispiel: Wir wollen in $8$-Bit die folgende Subtraktion durchführen: +**Beispiel 1:** Wir wollen in $8$-Bit die folgende Subtraktion durchführen: 
-$$0110\,1001 - 0001\,1101$$+$$0110\,1001 - 0001\,1101 = ?$$
 Bei der Subtraktion wird die erste Zahl Minuend, die Zweite Subtrahend genannt. Bei der Subtraktion wird die erste Zahl Minuend, die Zweite Subtrahend genannt.
  
Zeile 73: Zeile 74:
 $$0110\,1001 - 0001\,1101 = 0100\,1100$$ $$0110\,1001 - 0001\,1101 = 0100\,1100$$
  
 +**Beispiel 2:** Wir wollen berechnen: $$1\,1011 - 101 = ?$$
 +Beachte, dass hier *keine* Angaben bzgl. Anzahl Bits gemacht werden. Wir fassen deshalb beide Zahlen ($1\,1011$ und $101$) als *positive* Zahlen, auch wenn die Bits ganz links eine $1$ sind. Deshalb müssen wir die beiden Zahlen so erweitern, dass:
  
-=== Beispiel binäre Subtraktion ===+   1. beide gleich lang sind 
 +   1. beide links (mind.) eine $0$ haben
  
-Wir wollen berechnen 1101001 - 11101, wobei beide Zahlen als positive Zahlen aufzufassen sind. +Also notieren also beide Zahlen als $6-$Bit Zahlen: 
-Bei der Subtraktion wird die erste Zahl Minuenddie Zweite Subtrahend genannt.+$$01\,1011 - 00\,0101 = ?$$ 
 +Jetzt können wir vorgehen wie in Beispiel 1:
  
-   1. Befülle mit 0, so dass beide gleich lang: 1101001 - 0011101 +   1. 2-er Komplement (Invertieren, $+1$): $11\,1011$ 
-   1. Füge links ein zusäzliches Bit hinzu, damit wir auch negative Zahlen haben können: 01101001 - 00011101 +   1. Addieren$$01\,1011 11\,1011 101\,0110$$ 
-   1. Finde 2er-Komplement von Subtrahend (Invertieren, +1): 11100011 +   1. Zusätzliches 7. Bit ignorieren. Da keine feste Bitzahl angegeben wurdekann man auch noch die Nullen links ignorieren. Resultat ist also$$1\,1011 - 101 = 1\,0110$$ 
-   1. Addiere Minuend mit 2er-Komplement von Subtrahend01101001 11100011 ... + 
-   1. Entferne Bit ganz linksda dieses nur für die Berechnung verwendet wurde1001100+$$01\,1011 - 00\,0101 = ?$$
  
  
Zeile 94: Zeile 99:
  
 ===== Lösungen ===== ===== Lösungen =====
- 
-<nodisp 1> 
  
 ++++Binary to Decimal| ++++Binary to Decimal|
Zeile 145: Zeile 148:
  
 <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 173: Zeile 179:
         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 184: Zeile 191:
         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 196: Zeile 206:
     while len(b1) > len(b2):     while len(b1) > len(b2):
         b2 = '0' + b2         b2 = '0' + b2
-     +  
-    # add additinal zero at left (for 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 211: Zeile 238:
 ++++ ++++
  
-</nodisp> 
  
  
Zeile 326: Zeile 352:
 </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.1685188900.txt.gz
  • Zuletzt geändert: 2023-05-27 12:01
  • von sca