Ich bin ein Neuling sowohl bei Python3 als auch bein Programmieren an sich. Programmieren tu ich mit Gedit und python3 3.3.1 im Terminal(Betriebsystem ist Lubuntu).
Auf Wikipedia hab ich mir das Umrechnen von Dezimal in Binaer Zahlen angesehen. Die erste Varieante mit Modulo hab ich hin gekriegt.
Code: Alles auswählen
dez_zahl = 127
bin_zahl = []
a = dez_zahl
while a > 0:
quotient = a / 2
quotient = int(quotient)
rest = a % 2
if rest == 1:
bin_zahl.append(1)
else:
bin_zahl.append(0)
a = a - quotient - rest
print ("dez_zahl = ",dez_zahl," bin_zahl = ",bin_zahl)
Das Fuktioniert auch, aber die zweite Varieante versteh ich nicht.
Genauer wie soll das mit der Wertigkeit funktionieren.
Code: Alles auswählen
dez_zahl = 127 # 01111111 binaer
bin_zahl = []
zwpot = 0
x = 0
while zwpot < dez_zahl:
zwpot = pow(2,x)
x = x + 1
'''
die Schleife müsste bei 2^7 = 128 abbrechen, x = 8
wenn ich das richtig verstehe s = 127(dez) - 128(zwpot) --> -1
'''
Vielen Dank für eure Hilfe