Sina Media Lab
commited on
Commit
·
835f4c4
1
Parent(s):
c7d4159
Updates
Browse files
modules/number_system/twos_complement.py
CHANGED
|
@@ -2,10 +2,15 @@ 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 |
def calculate_twos_complement(number):
|
| 11 |
# Calculate 1's complement
|
|
@@ -13,7 +18,7 @@ def generate_question():
|
|
| 13 |
|
| 14 |
# Calculate 2's complement
|
| 15 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
| 16 |
-
twos_complement = twos_complement.zfill(
|
| 17 |
|
| 18 |
return ones_complement, twos_complement
|
| 19 |
|
|
@@ -22,13 +27,13 @@ def generate_question():
|
|
| 22 |
|
| 23 |
# Generate incorrect answers
|
| 24 |
while len(options) < 4:
|
| 25 |
-
invalid_number = ''.join(random.choice('01') for _ in range(
|
| 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 |
# Generate a step-by-step solution using SymPy
|
| 34 |
num_expr = sp.sympify(f'0b{number}')
|
|
@@ -39,11 +44,11 @@ def generate_question():
|
|
| 39 |
f"Step 1: Start with the original binary number: {number}",
|
| 40 |
f"Step 2: Find the 1's complement by flipping all bits: {ones_complement}",
|
| 41 |
f"Step 3: Add 1 to the 1's complement:",
|
| 42 |
-
f" {ones_complement} + 1 = {bin(twos_expr)[2:].zfill(
|
| 43 |
f"Step 4: The 2's complement of {number} is {correct_answer}."
|
| 44 |
]
|
| 45 |
|
| 46 |
-
explanation = f"The 2's complement of {number} is {correct_answer}. It is calculated by inverting the bits and adding 1."
|
| 47 |
|
| 48 |
return {
|
| 49 |
"question": question,
|
|
@@ -52,3 +57,19 @@ def generate_question():
|
|
| 52 |
"explanation": explanation,
|
| 53 |
"step_by_step_solution": step_by_step_solution
|
| 54 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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. The number of bits used for 2's complement calculations can vary."
|
| 6 |
|
| 7 |
+
def generate_question(num_bits):
|
| 8 |
+
# Ensure num_bits is a positive integer
|
| 9 |
+
if num_bits <= 0:
|
| 10 |
+
raise ValueError("Number of bits must be a positive integer.")
|
| 11 |
+
|
| 12 |
+
# Generate a random binary number with the given number of bits
|
| 13 |
+
number = ''.join(random.choice('01') for _ in range(num_bits))
|
| 14 |
|
| 15 |
def calculate_twos_complement(number):
|
| 16 |
# Calculate 1's complement
|
|
|
|
| 18 |
|
| 19 |
# Calculate 2's complement
|
| 20 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
| 21 |
+
twos_complement = twos_complement.zfill(num_bits)
|
| 22 |
|
| 23 |
return ones_complement, twos_complement
|
| 24 |
|
|
|
|
| 27 |
|
| 28 |
# Generate incorrect answers
|
| 29 |
while len(options) < 4:
|
| 30 |
+
invalid_number = ''.join(random.choice('01') for _ in range(num_bits))
|
| 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} (using {num_bits} bits)?"
|
| 37 |
|
| 38 |
# Generate a step-by-step solution using SymPy
|
| 39 |
num_expr = sp.sympify(f'0b{number}')
|
|
|
|
| 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(num_bits)}",
|
| 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. Note that this calculation is performed using {num_bits} bits."
|
| 52 |
|
| 53 |
return {
|
| 54 |
"question": question,
|
|
|
|
| 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")
|