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
26 changes: 26 additions & 0 deletions HW7/BinaryRepresentation/Makefile
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions HW7/BinaryRepresentation/include/logic.h
Original file line number Diff line number Diff line change
@@ -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);
39 changes: 39 additions & 0 deletions HW7/BinaryRepresentation/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "include/logic.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
59 changes: 59 additions & 0 deletions HW7/BinaryRepresentation/src/logic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "../include/logic.h"
#include <stdio.h>
#include <stdlib.h>

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;
}
107 changes: 107 additions & 0 deletions HW7/BinaryRepresentation/tests/tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "../include/logic.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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