Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/binary_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,53 @@
# Insert first number: 8
# The binary number is: 1000
#


def division(a: int) -> str:
b = ""
while a > 0:
if a % 2 == 0:
b = "0" + b
else:
b = "1" + b
a = a // 2
return f"{b}"


def carry(a: str) -> str:
result_list = list(a) # conversione dell'array in lista

for i in range(len(result_list) - 1, -1, -1): # gestione del riporto di 1
if result_list[i] == "0":
result_list[i] = "1"
break
result_list[i] = "0"

a = "".join(result_list)
return a


def invert(res: str) -> str:
res = res.zfill(16)
res = res.replace("0", "&").replace("1", "0").replace("&", "1")
return res


if __name__ == "__main__":
result = ""
input = int(input("Inserisci il numero da convertire: "))

if input == 0:
print("0")

elif input < 0:
input = -input
result = division(input)
result = invert(result)

print(f"Stringa modificata: {carry(result)}")
print("Il seguente numero è calcolato con la tecnica del complemento a due.")
print("Il risultato è rappresentato in 16 bit.")

elif input >= 0:
print(division(input))
49 changes: 49 additions & 0 deletions tests/test_binary_conv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import List
import pytest
from src import binary_converter

parametri: List[dict] = [
{"a": 5, "exp_ris": f"{bin(5)[2:]}", "bin_post_carry": list("110")},
{"a": 1, "exp_ris": f"{bin(1)[2:]}", "bin_post_carry": list("0")},
{"a": 8, "exp_ris": f"{bin(8)[2:]}", "bin_post_carry": list("1001")},
{"a": 111, "exp_ris": f"{bin(111)[2:]}", "bin_post_carry": list("1110000")},
{"a": 2.5, "exp_ris": f"{bin(round(2.5))[2:]}", "bin_post_carry": list("11")},
{
"a": 4.0000000,
"exp_ris": f"{bin(int(4.0000000))[2:]}",
"bin_post_carry": list("101"),
},
{"a": -1, "exp_ris": f"{bin(-1)[3:]}", "bin_post_carry": list("0")},
{
"a": 101.0110,
"exp_ris": f"{bin(int(101.0110))[2:]}",
"bin_post_carry": list("1100110"),
},
]

param_invert: List[dict] = [
{"input": "1", "exp_out": "1111111111111110"},
{"input": "", "exp_out": "1111111111111111"},
{"input": "10101010", "exp_out": "1111111101010101"},
{"input": "1010101010101010", "exp_out": "0101010101010101"},
{"input": "1111111111111111", "exp_out": "0000000000000000"},
{"input": "10101010101010101", "exp_out": "01010101010101010"},
]


@pytest.mark.parametrize("test", parametri)
def test_division(test: dict) -> int:
act_ris = binary_converter.division(int(abs(test["a"])))
assert test["exp_ris"] == act_ris


@pytest.mark.parametrize("test", parametri)
def test_carry(test: dict) -> str:
act_ris = binary_converter.carry(test["exp_ris"])
assert "".join(test["bin_post_carry"]) == act_ris


@pytest.mark.parametrize("test", param_invert)
def test_invert(test: dict) -> str:
act_ris = binary_converter.invert(test["input"])
assert test["exp_out"] == act_ris
7 changes: 0 additions & 7 deletions tests/test_sum.py

This file was deleted.

Loading