From 1f2ed35c44927fcd7c39bcbd71b0fc07eeea3333 Mon Sep 17 00:00:00 2001 From: deadvey Date: Fri, 15 Nov 2024 11:38:15 +0000 Subject: [PATCH] two's compliment --- bases-game.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/bases-game.py b/bases-game.py index 335e509..a7cfcd1 100644 --- a/bases-game.py +++ b/bases-game.py @@ -19,8 +19,35 @@ while True: Floating point 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: print("What is this binary floating point number in denery?\n") int_length = random.randint(0,7)