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-27 12:06] – [Subraktion] sca | gf_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 79: | Zeile 80: | ||
1. beide links (mind.) eine $0$ haben | 1. beide links (mind.) eine $0$ haben | ||
- | Also notieren | + | Also notieren |
$$01\,1011 - 00\,0101 = ?$$ | $$01\,1011 - 00\,0101 = ?$$ | ||
Jetzt können wir vorgehen wie in Beispiel 1: | Jetzt können wir vorgehen wie in Beispiel 1: | ||
+ | |||
+ | 1. 2-er Komplement (Invertieren, | ||
+ | 1. Addieren: $$01\,1011 + 11\,1011 = 101\,0110$$ | ||
+ | 1. Zusätzliches 7. Bit ignorieren. Da keine feste Bitzahl angegeben wurde, kann man auch noch die Nullen links ignorieren. Resultat ist also: $$1\,1011 - 101 = 1\,0110$$ | ||
$$01\,1011 - 00\,0101 = ?$$ | $$01\,1011 - 00\,0101 = ?$$ | ||
- | |||
- | === Beispiel binäre Subtraktion === | ||
- | |||
- | Wir wollen berechnen 1101001 - 11101, wobei beide Zahlen als positive Zahlen aufzufassen sind. | ||
- | Bei der Subtraktion wird die erste Zahl Minuend, die Zweite Subtrahend genannt. | ||
- | |||
- | 1. Befülle mit 0, so dass beide gleich lang: 1101001 - 0011101 | ||
- | 1. Füge links ein zusäzliches Bit hinzu, damit wir auch negative Zahlen haben können: 01101001 - 00011101 | ||
- | 1. Finde 2er-Komplement von Subtrahend (Invertieren, | ||
- | 1. Addiere Minuend mit 2er-Komplement von Subtrahend: 01101001 + 11100011 = ... | ||
- | 1. Entferne Bit ganz links, da dieses nur für die Berechnung verwendet wurde: 1001100 | ||
Zeile 105: | Zeile 99: | ||
===== Lösungen ===== | ===== Lösungen ===== | ||
- | |||
- | <nodisp 1> | ||
++++Binary to Decimal| | ++++Binary to Decimal| | ||
Zeile 156: | Zeile 148: | ||
<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 184: | Zeile 179: | ||
summe = ' | summe = ' | ||
return summe | return summe | ||
+ | |||
def invert(b): | def invert(b): | ||
+ | b = b.replace(" | ||
inv = "" | inv = "" | ||
i = 0 | i = 0 | ||
Zeile 195: | Zeile 191: | ||
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 207: | Zeile 206: | ||
while len(b1) > len(b2): | while len(b1) > len(b2): | ||
b2 = ' | b2 = ' | ||
- | | + | |
- | # add additinal | + | # add additional |
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 222: | Zeile 238: | ||
++++ | ++++ | ||
- | </ | ||
Zeile 337: | Zeile 352: | ||
</ | </ | ||
++++ | ++++ | ||
+ | |||
+ | ++++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(" | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
</ | </ | ||