Skip to content

Comments

ДЗ № 12.1 Двоичное представление, Шестаков Николай#16

Open
NicholayShestakov wants to merge 9 commits intomainfrom
binary_representation
Open

ДЗ № 12.1 Двоичное представление, Шестаков Николай#16
NicholayShestakov wants to merge 9 commits intomainfrom
binary_representation

Conversation

@NicholayShestakov
Copy link
Owner

Add

  • Binary representation for int and sum operation for binary ints

Comment on lines +14 to +17
if ((1 << i) & decimal) {
binary[31 - i] = '1';
} else {
binary[31 - i] = '0';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ой, ну, так тоже можно, но не проще ли хранить прямо числа 0 и 1 в char (или вообще сделать `bool)?


char* decimalToBinary(int32_t decimal)
{
char* binary = malloc(32 * sizeof(char));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо бы это обернуть в структуру, чтобы пользователь ненароком в другие функции не передал массив из 2 или 122 элементов

return binary;
}

char* binarySum(char* binaryFirst, char* binarySecond)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь получается не очень честно, потому что у Вас может получиться двойка. А если перейти на язык
электроники, то без этого можно обойтись :) https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder

Comment on lines +75 to +77
if (binary[0] == '1') {
++decimal;
decimal *= -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может просто вычесть 2^31?

#include <stdlib.h>
#include <string.h>

void test()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо бы иметь побольше тестов, но ладно

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants