Sina Media Lab
commited on
Commit
·
3540df9
1
Parent(s):
62acc4d
Updates
Browse files
modules/number_system/negative_binary.py
CHANGED
|
@@ -1,68 +1,61 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
title = "Negative Binary Numbers"
|
| 4 |
description = "This module covers negative binary numbers and their representation."
|
| 5 |
|
| 6 |
def generate_question():
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
number =
|
| 14 |
-
|
| 15 |
-
def calculate_negative_binary(
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Generate incorrect answers
|
| 27 |
while len(options) < 4:
|
| 28 |
-
|
| 29 |
-
if
|
| 30 |
-
options.append(
|
| 31 |
|
| 32 |
random.shuffle(options)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
]
|
| 43 |
-
else:
|
| 44 |
-
decimal_value = calculate_decimal_value(number)
|
| 45 |
-
question = f"What is the decimal value of the {bit_length}-bit binary number {number}?"
|
| 46 |
-
explanation = f"The decimal value of the {bit_length}-bit binary number {number} is {decimal_value}."
|
| 47 |
-
step_by_step_solution = [
|
| 48 |
-
f"Step 1: Convert each bit of the binary number {number} to its decimal equivalent.",
|
| 49 |
-
f"Step 2: Sum the values considering their place values."
|
| 50 |
-
]
|
| 51 |
-
correct_answer = str(decimal_value)
|
| 52 |
-
options = [correct_answer]
|
| 53 |
-
|
| 54 |
-
# Generate incorrect decimal answers
|
| 55 |
-
while len(options) < 4:
|
| 56 |
-
invalid_decimal = str(random.randint(0, 2 ** bit_length - 1))
|
| 57 |
-
if invalid_decimal != correct_answer:
|
| 58 |
-
options.append(invalid_decimal)
|
| 59 |
-
|
| 60 |
-
random.shuffle(options)
|
| 61 |
|
| 62 |
return {
|
| 63 |
"question": question,
|
| 64 |
"options": options,
|
| 65 |
-
"correct_answer":
|
| 66 |
"explanation": explanation,
|
| 67 |
"step_by_step_solution": step_by_step_solution
|
| 68 |
}
|
|
|
|
| 1 |
+
import random
|
| 2 |
|
| 3 |
title = "Negative Binary Numbers"
|
| 4 |
description = "This module covers negative binary numbers and their representation."
|
| 5 |
|
| 6 |
def generate_question():
|
| 7 |
+
bit_lengths = [2, 3, 4, 5, 6, 7, 8]
|
| 8 |
+
num_bits = random.choice(bit_lengths)
|
| 9 |
+
|
| 10 |
+
def generate_binary_number(length):
|
| 11 |
+
return ''.join(random.choice('01') for _ in range(length))
|
| 12 |
+
|
| 13 |
+
number = generate_binary_number(num_bits)
|
| 14 |
+
|
| 15 |
+
def calculate_negative_binary(binary_str):
|
| 16 |
+
# Interpret the binary string as a signed number
|
| 17 |
+
if binary_str[0] == '1':
|
| 18 |
+
# Two's complement
|
| 19 |
+
ones_complement = ''.join('1' if bit == '0' else '0' for bit in binary_str)
|
| 20 |
+
twos_complement = bin(int(ones_complement, 2) + 1)[2:].zfill(len(binary_str))
|
| 21 |
+
decimal_value = -int(twos_complement, 2)
|
| 22 |
+
else:
|
| 23 |
+
decimal_value = int(binary_str, 2)
|
| 24 |
+
|
| 25 |
+
return decimal_value
|
| 26 |
+
|
| 27 |
+
def decimal_to_binary(value, length):
|
| 28 |
+
# Convert a decimal number to binary with a given length
|
| 29 |
+
if value < 0:
|
| 30 |
+
value = (1 << length) + value
|
| 31 |
+
return bin(value)[2:].zfill(length)
|
| 32 |
+
|
| 33 |
+
correct_decimal = calculate_negative_binary(number)
|
| 34 |
+
correct_binary = decimal_to_binary(correct_decimal, num_bits)
|
| 35 |
+
|
| 36 |
+
options = [correct_decimal]
|
| 37 |
+
|
| 38 |
# Generate incorrect answers
|
| 39 |
while len(options) < 4:
|
| 40 |
+
random_decimal = random.randint(-2**(num_bits-1), 2**(num_bits-1)-1)
|
| 41 |
+
if random_decimal != correct_decimal:
|
| 42 |
+
options.append(random_decimal)
|
| 43 |
|
| 44 |
random.shuffle(options)
|
| 45 |
|
| 46 |
+
question = f"What is the decimal value of the signed {num_bits}-bit binary number {number}?"
|
| 47 |
+
explanation = f"The decimal value of the signed {num_bits}-bit binary number {number} is {correct_decimal}."
|
| 48 |
+
step_by_step_solution = [
|
| 49 |
+
f"Step 1: Identify if the binary number is positive or negative. The leftmost bit indicates the sign.",
|
| 50 |
+
f"Step 2: If the leftmost bit is '1', calculate the two's complement to find the magnitude of the negative number.",
|
| 51 |
+
f"Step 3: Convert the binary number to decimal considering its sign.",
|
| 52 |
+
f"Step 4: The decimal value of the signed {num_bits}-bit binary number {number} is {correct_decimal}."
|
| 53 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
return {
|
| 56 |
"question": question,
|
| 57 |
"options": options,
|
| 58 |
+
"correct_answer": correct_decimal,
|
| 59 |
"explanation": explanation,
|
| 60 |
"step_by_step_solution": step_by_step_solution
|
| 61 |
}
|