Avatar of vlzware

vlzware's solution

to Binary in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.

Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.

Note

  • Implement the conversion yourself. Do not use something else to perform the conversion for you.

About Binary (Base-2)

Decimal is a base-10 system.

A number 23 in base 10 notation can be understood as a linear combination of powers of 10:

  • The rightmost digit gets multiplied by 10^0 = 1
  • The next number gets multiplied by 10^1 = 10
  • ...
  • The nth number gets multiplied by 10^(n-1).
  • All these values are summed.

So: 23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10

Binary is similar, but uses powers of 2 rather than powers of 10.

So: 101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10.

Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_binary.c

#include "vendor/unity.h"
#include "../src/binary.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_b0_is_decimal_0(void)
{
   TEST_ASSERT_EQUAL(0, convert("0"));
}

void test_b1_is_decimal_1(void)
{
   TEST_IGNORE();               // delete this line to run test
   TEST_ASSERT_EQUAL(1, convert("1"));
}

void test_b10_is_decimal_2(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(2, convert("10"));
}

void test_b11_is_decimal_3(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(3, convert("11"));
}

void test_b100_is_decimal_4(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(4, convert("100"));
}

void test_b1001_is_decimal_9(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(9, convert("1001"));
}

void test_b10001101000_is_decimal_1128(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(1128, convert("10001101000"));
}

void test_b11010_is_decimal_26(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(26, convert("11010"));
}

void test_binary_ignores_leading_zeros(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(31, convert("00000000011111"));
}

void test_2_is_not_a_valid_binary_digit(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("2"));
}

void test_a_number_containing_a_non_binary_digit_is_invalid(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("1002001"));
}

void test_a_number_with_trailing_non_binary_characters_is_invalid(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("10nope"));
}

void test_a_number_with_leading_non_binary_characters_is_invalid(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("nope10"));
}

void test_a_number_with_internal_non_binary_characters_is_invalid(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("10nope10"));
}

void test_a_number_and_a_word_whitespace_spearated_is_invalid(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(INVALID, convert("001 nope"));
}

int main(void)
{
   UnityBegin("test/test_binary.c");
   RUN_TEST(test_b0_is_decimal_0);
   RUN_TEST(test_b1_is_decimal_1);
   RUN_TEST(test_b10_is_decimal_2);
   RUN_TEST(test_b11_is_decimal_3);
   RUN_TEST(test_b100_is_decimal_4);
   RUN_TEST(test_b1001_is_decimal_9);
   RUN_TEST(test_b11010_is_decimal_26);
   RUN_TEST(test_b10001101000_is_decimal_1128);
   RUN_TEST(test_binary_ignores_leading_zeros);
   RUN_TEST(test_2_is_not_a_valid_binary_digit);
   RUN_TEST(test_a_number_containing_a_non_binary_digit_is_invalid);
   RUN_TEST(test_a_number_with_trailing_non_binary_characters_is_invalid);
   RUN_TEST(test_a_number_with_leading_non_binary_characters_is_invalid);
   RUN_TEST(test_a_number_with_internal_non_binary_characters_is_invalid);
   RUN_TEST(test_a_number_and_a_word_whitespace_spearated_is_invalid);
   UnityEnd();
   return 0;
}

src/binary.c

#include "binary.h"
#include <string.h>

int convert(const char *binary)
{
	if (binary == NULL)
		return INVALID;

	int len = strlen(binary);
	if (len == 0)
		return INVALID;

	int res = 0;
	int i;

	for (i = 0; i < len; i++)
		if (binary[i] == '1')
			res |= 1 << (len - i - 1);
		else if (binary[i] != '0')
			return INVALID;

	return res;
}

src/binary.h

#ifndef BINARY_H
#define BINARY_H

#define INVALID -1
int convert(const char *binary);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?