two's compliment
This commit is contained in:
parent
acff092e30
commit
1f2ed35c44
@ -19,8 +19,35 @@ while True:
|
|||||||
Floating point binary
|
Floating point binary
|
||||||
Twos compliment, Sign & Magnitude, Complex binary
|
Twos compliment, Sign & Magnitude, Complex binary
|
||||||
'''
|
'''
|
||||||
type_int = random.randint(0,2)
|
type_int = random.randint(0,3)
|
||||||
|
|
||||||
|
if type_int == 3:
|
||||||
|
start_base = random.randint(0,1)
|
||||||
|
positive_int = random.randint(0,127)
|
||||||
|
negative_int = 0 - positive_int
|
||||||
|
positive_bin = format(positive_int, '#010b')[2:]
|
||||||
|
negative_bin = ""
|
||||||
|
for x in positive_bin:
|
||||||
|
if x == "0":
|
||||||
|
negative_bin += "1"
|
||||||
|
else:
|
||||||
|
negative_bin += "0"
|
||||||
|
|
||||||
|
negative_bin = bin(int(negative_bin,2) + 1)[2:]
|
||||||
|
if (start_base == 0): # Binary -> Denery
|
||||||
|
print("\nWhat does this two's compliment number represent in base 10?\n")
|
||||||
|
print(negative_bin)
|
||||||
|
answer = str(negative_int)
|
||||||
|
elif (start_base == 1): # Denery -> Binary
|
||||||
|
print("\nWhat does this negative number represent in base 2 (two's compliment)\n")
|
||||||
|
print(negative_int)
|
||||||
|
answer = str(negative_bin)
|
||||||
|
|
||||||
|
attempt = input()
|
||||||
|
if attempt == answer:
|
||||||
|
correct()
|
||||||
|
else:
|
||||||
|
print(f"No, the answer was {answer}")
|
||||||
if type_int == 2:
|
if type_int == 2:
|
||||||
print("What is this binary floating point number in denery?\n")
|
print("What is this binary floating point number in denery?\n")
|
||||||
int_length = random.randint(0,7)
|
int_length = random.randint(0,7)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user