Sina Media Lab
commited on
Commit
·
7172720
1
Parent(s):
835f4c4
Updates
Browse files
modules/number_system/twos_complement.py
CHANGED
@@ -2,40 +2,34 @@ import random
|
|
2 |
import sympy as sp
|
3 |
|
4 |
title = "2's Complement Questions"
|
5 |
-
description = "This module explains the 2's complement method for representing negative numbers.
|
6 |
|
7 |
-
def generate_question(
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Generate a random binary number
|
13 |
-
number = ''.join(random.choice('01') for _ in range(
|
14 |
|
15 |
def calculate_twos_complement(number):
|
16 |
-
# Calculate 1's complement
|
17 |
ones_complement = ''.join('1' if bit == '0' else '0' for bit in number)
|
18 |
-
|
19 |
-
# Calculate 2's complement
|
20 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
21 |
-
twos_complement = twos_complement.zfill(
|
22 |
-
|
23 |
return ones_complement, twos_complement
|
24 |
|
25 |
ones_complement, correct_answer = calculate_twos_complement(number)
|
26 |
options = [correct_answer]
|
27 |
|
28 |
-
# Generate incorrect answers
|
29 |
while len(options) < 4:
|
30 |
-
invalid_number = ''.join(random.choice('01') for _ in range(
|
31 |
if invalid_number != correct_answer:
|
32 |
options.append(invalid_number)
|
33 |
|
34 |
random.shuffle(options)
|
35 |
|
36 |
-
question = f"What is the 2's complement of the binary number {number}
|
37 |
-
|
38 |
-
# Generate a step-by-step solution using SymPy
|
39 |
num_expr = sp.sympify(f'0b{number}')
|
40 |
ones_expr = sp.sympify(f'0b{ones_complement}')
|
41 |
twos_expr = ones_expr + 1
|
@@ -44,11 +38,11 @@ def generate_question(num_bits):
|
|
44 |
f"Step 1: Start with the original binary number: {number}",
|
45 |
f"Step 2: Find the 1's complement by flipping all bits: {ones_complement}",
|
46 |
f"Step 3: Add 1 to the 1's complement:",
|
47 |
-
f" {ones_complement} + 1 = {bin(twos_expr)[2:].zfill(
|
48 |
f"Step 4: The 2's complement of {number} is {correct_answer}."
|
49 |
]
|
50 |
|
51 |
-
explanation = f"The 2's complement of {number} is {correct_answer}. It is calculated by inverting the bits and adding 1.
|
52 |
|
53 |
return {
|
54 |
"question": question,
|
@@ -57,19 +51,3 @@ def generate_question(num_bits):
|
|
57 |
"explanation": explanation,
|
58 |
"step_by_step_solution": step_by_step_solution
|
59 |
}
|
60 |
-
|
61 |
-
# Example usage
|
62 |
-
num_bits_list = [4, 8, 16]
|
63 |
-
questions = {num_bits: generate_question(num_bits) for num_bits in num_bits_list}
|
64 |
-
|
65 |
-
# Display the generated questions and their descriptions
|
66 |
-
for num_bits, q in questions.items():
|
67 |
-
print(f"Number of bits: {num_bits}")
|
68 |
-
print("Question:", q["question"])
|
69 |
-
print("Options:", q["options"])
|
70 |
-
print("Correct Answer:", q["correct_answer"])
|
71 |
-
print("Explanation:", q["explanation"])
|
72 |
-
print("Step-by-Step Solution:")
|
73 |
-
for step in q["step_by_step_solution"]:
|
74 |
-
print(step)
|
75 |
-
print("\n" + "-"*40 + "\n")
|
|
|
2 |
import sympy as sp
|
3 |
|
4 |
title = "2's Complement Questions"
|
5 |
+
description = "This module explains the 2's complement method for representing negative numbers."
|
6 |
|
7 |
+
def generate_question():
|
8 |
+
# Choose a random bit length from 4, 5, 6, and 8 bits
|
9 |
+
bit_lengths = [4, 5, 6, 8]
|
10 |
+
bit_length = random.choice(bit_lengths)
|
11 |
+
|
12 |
+
# Generate a random binary number of the selected bit length
|
13 |
+
number = ''.join(random.choice('01') for _ in range(bit_length))
|
14 |
|
15 |
def calculate_twos_complement(number):
|
|
|
16 |
ones_complement = ''.join('1' if bit == '0' else '0' for bit in number)
|
|
|
|
|
17 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
18 |
+
twos_complement = twos_complement.zfill(len(number))
|
|
|
19 |
return ones_complement, twos_complement
|
20 |
|
21 |
ones_complement, correct_answer = calculate_twos_complement(number)
|
22 |
options = [correct_answer]
|
23 |
|
|
|
24 |
while len(options) < 4:
|
25 |
+
invalid_number = ''.join(random.choice('01') for _ in range(bit_length))
|
26 |
if invalid_number != correct_answer:
|
27 |
options.append(invalid_number)
|
28 |
|
29 |
random.shuffle(options)
|
30 |
|
31 |
+
question = f"What is the 2's complement of the binary number {number}?"
|
32 |
+
|
|
|
33 |
num_expr = sp.sympify(f'0b{number}')
|
34 |
ones_expr = sp.sympify(f'0b{ones_complement}')
|
35 |
twos_expr = ones_expr + 1
|
|
|
38 |
f"Step 1: Start with the original binary number: {number}",
|
39 |
f"Step 2: Find the 1's complement by flipping all bits: {ones_complement}",
|
40 |
f"Step 3: Add 1 to the 1's complement:",
|
41 |
+
f" {ones_complement} + 1 = {bin(twos_expr)[2:].zfill(len(number))}",
|
42 |
f"Step 4: The 2's complement of {number} is {correct_answer}."
|
43 |
]
|
44 |
|
45 |
+
explanation = f"The 2's complement of {number} is {correct_answer}. It is calculated by inverting the bits and adding 1."
|
46 |
|
47 |
return {
|
48 |
"question": question,
|
|
|
51 |
"explanation": explanation,
|
52 |
"step_by_step_solution": step_by_step_solution
|
53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|