From 4f4c389b363ec2d52aa8045c6007bdf74d4969f8 Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Mon, 3 Nov 2025 23:50:54 +0100 Subject: [PATCH 1/4] feat: inizialized workflow --- exercises/binary_converter.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 19bf37dd..5ad9a743 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -5,3 +5,18 @@ Insert first number: 8 The binary number is: 1000 */ +#include +using namespace std; + +unsigned int toBinary(int& num){ + //TODO: converting function +} + +int main(){ + int inputNum; + cout << "Insert first number: "; + cin >> inputNum; + + cout << "The binary number is: " << toBinary(inputNum) << endl; + return 0; +} \ No newline at end of file From 725f51a865dddf6c0e72238e14324bee18a5647e Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Tue, 4 Nov 2025 00:36:19 +0100 Subject: [PATCH 2/4] feat: implemented binary conversion --- exercises/binary_converter.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 5ad9a743..41e4b430 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -2,21 +2,33 @@ Write a program that given a number as input convert it in binary. Output: - Insert first number: 8 + Insert number: 8 The binary number is: 1000 */ #include using namespace std; -unsigned int toBinary(int& num){ - //TODO: converting function +void printBinary(int num){ + unsigned short arr[32]; //32 is the number of bits in a standard integer + size_t i; + for(i = 0; num > 0; i++){ + arr[i] = num % 2; //Saving the module inside of arr[i] + num /= 2; + } + + while (i > 0){ // 'i' contains the number of bits needed for the conversion + cout << arr[--i]; + } } int main(){ int inputNum; - cout << "Insert first number: "; + cout << "Insert number: "; cin >> inputNum; - cout << "The binary number is: " << toBinary(inputNum) << endl; + cout << "The binary number is: "; + printBinary(inputNum); + cout << endl; + return 0; } \ No newline at end of file From a3d893dfe4b565ea11aad2cadb16eeedd7a4d6f3 Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Tue, 4 Nov 2025 17:11:27 +0100 Subject: [PATCH 3/4] feat: implemented negative decimal conversion --- exercises/binary_converter.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 41e4b430..266048fe 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -9,11 +9,12 @@ using namespace std; void printBinary(int num){ + unsigned int unsigned_num = static_cast(num); unsigned short arr[32]; //32 is the number of bits in a standard integer size_t i; - for(i = 0; num > 0; i++){ - arr[i] = num % 2; //Saving the module inside of arr[i] - num /= 2; + for(i = 0; unsigned_num > 0; i++){ + arr[i] = unsigned_num % 2; //Saving the module inside of arr[i] + unsigned_num /= 2; } while (i > 0){ // 'i' contains the number of bits needed for the conversion From 2a317932c24dc1a036cd57f71daa060c1e5ae89d Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Wed, 5 Nov 2025 15:30:39 +0100 Subject: [PATCH 4/4] chore: fixed codestyle --- exercises/binary_converter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 266048fe..4b92e773 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -14,7 +14,7 @@ void printBinary(int num){ size_t i; for(i = 0; unsigned_num > 0; i++){ arr[i] = unsigned_num % 2; //Saving the module inside of arr[i] - unsigned_num /= 2; + unsigned_num /= 2; } while (i > 0){ // 'i' contains the number of bits needed for the conversion @@ -32,4 +32,4 @@ int main(){ cout << endl; return 0; -} \ No newline at end of file +}