math / modules /numbering_system /negative_binary.py
Sina Media Lab
Updates
74a6085
raw
history blame
1.34 kB
# modules/negative_binary.py
title = "Negative Binary Numbers"
description = "This module covers negative binary numbers and their representation."
def generate_question():
import random
number = ''.join(random.choice('01') for _ in range(8))
def calculate_negative_binary(number):
# Example logic: return a simple 2's complement representation
return bin(int(number, 2) * -1)[3:].zfill(len(number))
correct_answer = calculate_negative_binary(number)
options = [correct_answer]
# Generate incorrect answers
while len(options) < 4:
invalid_number = ''.join(random.choice('01') for _ in range(8))
if invalid_number != correct_answer:
options.append(invalid_number)
random.shuffle(options)
question = f"What is the negative representation of the binary number {number}?"
explanation = f"The negative binary representation of {number} is {correct_answer}."
step_by_step_solution = [
"Step 1: Determine the 2's complement of the binary number.",
"Step 2: The result is the negative binary representation."
]
return {
"question": question,
"options": options,
"correct_answer": correct_answer,
"explanation": explanation,
"step_by_step_solution": step_by_step_solution
}