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-06-01 11:50] – [Lösungen Code für Lehrpersonen] scagf_informatik:zahlensysteme [2023-06-06 21:30] (aktuell) – [Lösungen] sca
Zeile 99: Zeile 99:
  
 ===== Lösungen ===== ===== Lösungen =====
- 
-<nodisp 1> 
  
 ++++Binary to Decimal| ++++Binary to Decimal|
Zeile 150: 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 178: 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 189: 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 201: Zeile 206:
     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 238:
 ++++ ++++
  
-</nodisp> 
  
  
Zeile 356: Zeile 377:
 <code python> <code python>
 ### EXERCISE GENERATOR ### EXERCISE GENERATOR
- 
 for i in range(3): for i in range(3):
     da = random.randint(128,256)     da = random.randint(128,256)
Zeile 370: Zeile 390:
     a = bin(da)[2:]     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("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein. 2-er Komplement in 8-Bit von:\n" + a)
-    print("LOESUNG: " + invert(a) + "\n") +    print("LOESUNG: " + binary_complement(a) + "\n")
  
 for i in range(3): for i in range(3):
Zeile 392: Zeile 411:
     print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein.\n" + a + " * " + b )     print("Berechne schriftlich von Hand (Bemerkung & isPencil-Feld), trage Resultat ins Antwortfeld ein.\n" + a + " * " + b )
     print("LOESUNG: " + bin(da*db)[2:] + "\n")     print("LOESUNG: " + bin(da*db)[2:] + "\n")
- 
 </code> </code>
 ++++ ++++
  • gf_informatik/zahlensysteme.1685620234.txt.gz
  • Zuletzt geändert: 2023-06-01 11:50
  • von sca