Sina Media Lab
commited on
Commit
·
5f6edac
1
Parent(s):
9c228bf
Updates
Browse files
modules/number_system/twos_complement.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import random
|
2 |
import sympy as sp
|
3 |
|
@@ -5,45 +7,47 @@ 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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
18 |
-
|
19 |
-
return
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
while len(options) < 4:
|
25 |
-
invalid_number =
|
26 |
-
|
27 |
-
|
|
|
28 |
|
|
|
29 |
random.shuffle(options)
|
30 |
|
31 |
-
question = f"What is the 2's complement of the {
|
32 |
-
|
33 |
-
num_expr = sp.sympify(f'0b{number}')
|
34 |
-
ones_expr = sp.sympify(f'0b{ones_complement}')
|
35 |
-
twos_expr = ones_expr + 1
|
36 |
-
|
37 |
step_by_step_solution = [
|
38 |
-
f"Step 1:
|
39 |
-
f"Step 2:
|
40 |
-
f"Step 3:
|
41 |
-
f"
|
42 |
-
f"Step 4: The 2's complement of {number} is {correct_answer}."
|
43 |
]
|
44 |
|
45 |
-
explanation = f"The 2's complement of the {bit_length}-bit binary number {number} is {correct_answer}. It is calculated by inverting the bits and adding 1."
|
46 |
-
|
47 |
return {
|
48 |
"question": question,
|
49 |
"options": options,
|
|
|
1 |
+
# modules/number_system/twos_complement.py
|
2 |
+
|
3 |
import random
|
4 |
import sympy as sp
|
5 |
|
|
|
7 |
description = "This module explains the 2's complement method for representing negative numbers."
|
8 |
|
9 |
def generate_question():
|
10 |
+
bit_lengths = [4, 5, 6, 7, 8]
|
11 |
+
num_bits = random.choice(bit_lengths)
|
12 |
+
|
13 |
+
def generate_binary_number(length):
|
14 |
+
return ''.join(random.choice('01') for _ in range(length))
|
15 |
+
|
16 |
+
number = generate_binary_number(num_bits)
|
17 |
+
|
18 |
+
def calculate_twos_complement(binary_str, length):
|
19 |
+
if binary_str == '0' * length:
|
20 |
+
return binary_str # 2's complement of 0 is still 0
|
21 |
+
# Invert the bits (1's complement)
|
22 |
+
ones_complement = ''.join('1' if bit == '0' else '0' for bit in binary_str)
|
23 |
+
# Add 1 to the 1's complement
|
24 |
twos_complement = bin(int(ones_complement, 2) + 1)[2:]
|
25 |
+
# Ensure the result is truncated or zero-padded to fit the specified bit length
|
26 |
+
return twos_complement.zfill(length)[-length:]
|
27 |
+
|
28 |
+
correct_answer = calculate_twos_complement(number, num_bits)
|
29 |
+
|
30 |
+
options = {correct_answer}
|
31 |
+
|
32 |
+
# Generate incorrect answers
|
33 |
while len(options) < 4:
|
34 |
+
invalid_number = generate_binary_number(num_bits)
|
35 |
+
invalid_twos_complement = calculate_twos_complement(invalid_number, num_bits)
|
36 |
+
if invalid_twos_complement != correct_answer:
|
37 |
+
options.add(invalid_twos_complement)
|
38 |
|
39 |
+
options = list(options)
|
40 |
random.shuffle(options)
|
41 |
|
42 |
+
question = f"What is the 2's complement of the {num_bits}-bit binary number {number}?"
|
43 |
+
explanation = f"The 2's complement of the {num_bits}-bit binary number {number} is {correct_answer}."
|
|
|
|
|
|
|
|
|
44 |
step_by_step_solution = [
|
45 |
+
f"Step 1: Invert the bits (1's complement) of the binary number {number}.",
|
46 |
+
f"Step 2: Add 1 to the result to get the 2's complement.",
|
47 |
+
f"Step 3: Ensure the result is a {num_bits}-bit binary number by truncating or zero-padding if necessary.",
|
48 |
+
f"Step 4: The 2's complement of the {num_bits}-bit binary number {number} is {correct_answer}."
|
|
|
49 |
]
|
50 |
|
|
|
|
|
51 |
return {
|
52 |
"question": question,
|
53 |
"options": options,
|