diff --git a/HW7/BinaryRepresentation/Makefile b/HW7/BinaryRepresentation/Makefile new file mode 100644 index 0000000..4a41043 --- /dev/null +++ b/HW7/BinaryRepresentation/Makefile @@ -0,0 +1,26 @@ +CC = gcc +CFLAGS = -Iinclude -Wall -Wextra + +SRC = main.c src/logic.c +TEST_SRC = tests/tests.c + +OUT = program +TEST_OUT = testProgram + +all: $(OUT) + +$(OUT): $(SRC) + $(CC) $(CFLAGS) $(SRC) -o $(OUT) + +test: src/logic.c tests/tests.c + $(CC) $(CFLAGS) -DTEST_MAIN src/logic.c tests/tests.c -o $(TEST_OUT) + +clean: + rm -f $(OUT) $(TEST_OUT) + +format: + find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file -i + +format-check: + find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file --dry-run -Werror + diff --git a/HW7/BinaryRepresentation/include/logic.h b/HW7/BinaryRepresentation/include/logic.h new file mode 100644 index 0000000..5f51588 --- /dev/null +++ b/HW7/BinaryRepresentation/include/logic.h @@ -0,0 +1,8 @@ +#pragma once + +extern const int BITS_NUM; + +char* toTwosComplement(int num); +void printBinary(const char* binaryStr); +char* binaryAddition(const char* firstBin, const char* secBin); +int toDecimal(const char* binaryStr); diff --git a/HW7/BinaryRepresentation/main.c b/HW7/BinaryRepresentation/main.c new file mode 100644 index 0000000..fd82548 --- /dev/null +++ b/HW7/BinaryRepresentation/main.c @@ -0,0 +1,39 @@ +#include "include/logic.h" +#include +#include +#include + +int main() +{ + int firstNum, secNum; + printf("First number = "); + if (scanf("%d", &firstNum) != 1) { + printf("Invalid input\n"); + return 1; + } + printf("Second number = "); + if (scanf("%d", &secNum) != 1) { + printf("Invalid input\n"); + return 1; + } + + char* firstBin = toTwosComplement(firstNum); + char* secBin = toTwosComplement(secNum); + printf("First binary:\n"); + printBinary(firstBin); + printf("Second binary:\n"); + printBinary(secBin); + + char* addition = binaryAddition(firstBin, secBin); + printf("Addition:\n"); + printBinary(addition); + + int decimal = toDecimal(addition); + printf("Decimal:\n%d", decimal); + + free(firstBin); + free(secBin); + free(addition); + + return 0; +} diff --git a/HW7/BinaryRepresentation/src/logic.c b/HW7/BinaryRepresentation/src/logic.c new file mode 100644 index 0000000..ad1235c --- /dev/null +++ b/HW7/BinaryRepresentation/src/logic.c @@ -0,0 +1,59 @@ +#include "../include/logic.h" +#include +#include + +const int BITS_NUM = sizeof(int) * 8; + +char* toTwosComplement(const int num) +{ + char* binaryStr = (char*)malloc(BITS_NUM + 1); + unsigned int mask = 1 << (BITS_NUM - 1); + + unsigned int uNum = (unsigned int)num; + + for (int i = 0; i < BITS_NUM; ++i) { + binaryStr[i] = (uNum & mask) ? '1' : '0'; + mask >>= 1; + } + binaryStr[BITS_NUM] = '\0'; + + return binaryStr; +} + +void printBinary(const char* binaryStr) +{ + for (int i = 0; i < BITS_NUM; ++i) { + printf("%c", binaryStr[i]); + if ((i + 1) % 8 == 0 && i != BITS_NUM - 1) + printf(" "); + } + printf("\n"); +} + +char* binaryAddition(const char* firstBin, const char* secBin) +{ + char* binaryStr = (char*)malloc(BITS_NUM + 1); + + int carry = 0; + for (int i = BITS_NUM - 1; i >= 0; --i) { + int firstBit = firstBin[i] - '0'; + int secBit = secBin[i] - '0'; + + int sum = firstBit + secBit + carry; + binaryStr[i] = (sum % 2) + '0'; + carry = sum / 2; + } + binaryStr[BITS_NUM] = '\0'; + + return binaryStr; +} + +int toDecimal(const char* binaryStr) +{ + unsigned int decimal = 0; + + for (int i = 0; i < BITS_NUM; ++i) + decimal = (decimal << 1) | (binaryStr[i] - '0'); + + return (int)decimal; +} diff --git a/HW7/BinaryRepresentation/tests/tests.c b/HW7/BinaryRepresentation/tests/tests.c new file mode 100644 index 0000000..cea0a1b --- /dev/null +++ b/HW7/BinaryRepresentation/tests/tests.c @@ -0,0 +1,107 @@ +#include "../include/logic.h" +#include +#include +#include + +int assertInt(const char* name, int got, int expected) +{ + if (got != expected) { + printf("Failed %s: got: %d, expected: %d\n", name, got, expected); + return 0; + } + printf("Passed %s\n", name); + return 1; +} + +int assertStr(const char* name, const char* got, const char* expected) +{ + if (strcmp(got, expected) != 0) { + printf("Failed %s: got: %s, expected: %s\n", name, got, expected); + return 0; + } + printf("Passed %s\n", name); + return 1; +} + +void testToTwosComplementPositive() +{ + char* b = toTwosComplement(5); + char expected[BITS_NUM + 1]; + for (int i = 0; i < BITS_NUM; ++i) + expected[i] = '0'; + expected[BITS_NUM - 3] = '1'; + expected[BITS_NUM - 1] = '1'; + expected[BITS_NUM] = '\0'; + assertStr("toTwosComplement(5)", b, expected); + free(b); +} + +void testToTwosComplementNegative() +{ + char* b = toTwosComplement(-1); + char expected[BITS_NUM + 1]; + for (int i = 0; i < BITS_NUM; ++i) + expected[i] = '1'; + expected[BITS_NUM] = '\0'; + assertStr("toTwosComplement(-1)", b, expected); + free(b); +} + +void testBinaryAdditionPositive() +{ + char* a = toTwosComplement(5); + char* b = toTwosComplement(7); + char* sum = binaryAddition(a, b); + int dec = toDecimal(sum); + assertInt("binaryAddition(5+7)", dec, 12); + free(a); + free(b); + free(sum); +} + +void testBinaryAdditionNegative() +{ + char* a = toTwosComplement(-5); + char* b = toTwosComplement(1); + char* sum = binaryAddition(a, b); + int dec = toDecimal(sum); + assertInt("binaryAddition(-5+1)", dec, -4); + free(a); + free(b); + free(sum); +} + +void testToDecimalZero() +{ + char* a = toTwosComplement(0); + int dec = toDecimal(a); + assertInt("toDecimal(0)", dec, 0); + free(a); +} + +void testToDecimalNegative() +{ + char* a = toTwosComplement(-1); + int dec = toDecimal(a); + assertInt("toDecimal(-1)", dec, -1); + free(a); +} + +void runTests() +{ + testToTwosComplementPositive(); + testToTwosComplementNegative(); + testBinaryAdditionPositive(); + testBinaryAdditionNegative(); + testToDecimalZero(); + testToDecimalNegative(); + printf("All tests finished.\n"); +} + +#ifdef TEST_MAIN +int main() +{ + runTests(); + return 0; +} +#endif