diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ff33cbf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*.c] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4b23c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.DS_STORE +*.o +*.log +*.gcov +*.gcda +*.gcno +binary +test +example +coverage diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3216cf8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: c +compiler: +# - clang +- gcc +script: make run-test +after_script: sudo pip install cpp-coveralls && make run-coverage && coveralls --exclude test.c + +notifications: + email: false diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0dc9fe3 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +TEST = test.c binary.c +EXAMPLE = example.c binary.c +OBJ_TEST = $(TEST:.c=.o) +OBJ_EXAMPLE = $(EXAMPLE:.c=.o) + +CFLAGS = -D_GNU_SOURCE -std=c99 + +LFLAGS = -Wall -Wno-format-y2k -W -Wstrict-prototypes \ + -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \ + -Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \ + -Wnested-externs -Wredundant-decls + +COVFLAGS = -Wall -fprofile-arcs -ftest-coverage + +test: $(OBJ_TEST) + $(CC) $(OBJ_TEST) -o $@ + +example: $(OBJ_EXAMPLE) + $(CC) $(OBJ_EXAMPLE) -o $@ + +coverage: $(OBJ_TEST) + gcc $(COVFLAGS) $(TEST) -o $@ + +.SUFFIXES: .c .o +.c.o: + $(CC) $< $(CFLAGS) $(LFLAGS) -c -o $@ + +run-coverage: coverage + ./coverage && gcov binary + +run-test: test + ./test + +run-example: example + ./example + +clean: + rm -f coverage test example $(OBJ_TEST) $(OBJ_EXAMPLE) *.gc{ov,da,no} + +.PHONY: clean run-coverage run-test diff --git a/binary.c b/binary.c new file mode 100644 index 0000000..136d4f3 --- /dev/null +++ b/binary.c @@ -0,0 +1,61 @@ +// +// An small library to work with +// binary numbers. +// +// binary.c +// +// MIT licensed. +// Copyright (c) Abraham Hernandez +// + +#include + +bool +is_binary (long long binary) { + bool status = true; + while(true) { + if (binary == 0) break; + else { + int tmp = binary % 10; + if(tmp > 1) { + status = false; + break; + } + binary = binary / 10; + } + } + return status; +} + +long +to_decimal (long long binary) { + if(!is_binary(binary)) return -1; + + int decimal = 0; + int multiplier = 1; + + while (binary != 0) { + decimal += (binary % 10) * multiplier; + binary /= 10; + multiplier *= 2; + } + return decimal; +} + +long long to_binary(long number) { + long long binary = 0; + int remainder; + int i = 1; + + while (number != 0) { + remainder = number % 2; + number /= 2; + binary += remainder * i; + i *= 10; + } + return binary; +} + +// TODO: +// add_binary() +// substract_binary() ... diff --git a/binary.h b/binary.h new file mode 100644 index 0000000..70bcb95 --- /dev/null +++ b/binary.h @@ -0,0 +1,29 @@ +#ifndef BINARY_H +#define BINARY_H + +// +// binary.h +// +// MIT licensed. +// Copyright (c) Abraham Hernandez +// + +#define bool int + +#ifdef __cplusplus +extern "C" { +#endif + +bool +is_binary(long binary); + +long +to_decimal(long long binary); + +long long to_binary(long number); + +#ifdef __cplusplus +} +#endif + +#endif // BINARY_H diff --git a/example.c b/example.c new file mode 100644 index 0000000..b0b75a0 --- /dev/null +++ b/example.c @@ -0,0 +1,8 @@ +#include +#include "binary.h" + +int main() { + printf("%d\n", is_binary(1010)); + printf("%ld\n", to_decimal(10000000011)); + printf("%lld\n", to_binary(1000)); +} diff --git a/license b/license new file mode 100644 index 0000000..9146125 --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) Abraham Hernandez (abranhe.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..af97db7 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "binary.c", + "version": "1.0.0", + "description": "An small library to work with binary numbers", + "license": "MIT", + "keywords": [ + "binary", "binary-numbers" + ], + "repo": "abranhe/binary.c", + "src": [ + "binary.h", + "binary.c" + ] +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d1988c5 --- /dev/null +++ b/readme.md @@ -0,0 +1,91 @@ +

+
+ +
+
+ binary.c: An small C library to work with binary numbers +
+

+ +

+ + + + + + +
+
+

+In mathematics and digital electronics, a binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically 0 and 1. + + +## Installation + +*Installing using [Clib](https://github.com/clibs/clib)* + +```sh +$ clib install abranhe/binary.c +``` + +## Usage + +```c +#include +#include "binary.h" + +int main() { + printf("%d\n", is_binary(1010)); + // 1 + printf("%ld\n", to_decimal(10000000011)); + // 1027 + printf("%lld\n", to_binary(1000)); + // 1111101000 +} +``` + +## API + +#### `bool is_binary(long binary);` + +*Returns true (`1`) if the number is binary, otherwise return false (`0`)* + +###### Params: + +- `binary`: long binary number + +#### `long to_decimal(long long binary);` + +*Returns a long decimal number from a binary number* + +###### Params: + +- `binary`: long binary number + +#### `long long to_binary(long decimal);` + +*Returns a binary number from a decimal number* + +###### Params: + +- `decimal`: a long decimal number + +## Team + +|[![Carlos Abraham Logo][abranhe-img]][abranhe]| +| :-: | +| [Carlos Abraham][abranhe] | + +## License + +[MIT][license] License © [Carlos Abraham][abranhe] + + +[abranhe]: https://github.com/abranhe +[abranhe-img]: https://avatars3.githubusercontent.com/u/21347264?s=50 +[license]: https://github.com/abranhe/binary.c/blob/master/license +[example]: https://github.com/abranhe/binary.c/blob/master/example.c +[travis-badge]: https://img.shields.io/travis/abranhe/binary.c.svg +[travis-status]: https://travis-ci.org/abranhe/binary.c +[coverage-badge]: https://img.shields.io/coveralls/abranhe/binary.c.svg +[coverage-status]: https://coveralls.io/r/abranhe/binary.c?branch=master diff --git a/test.c b/test.c new file mode 100644 index 0000000..f140ce8 --- /dev/null +++ b/test.c @@ -0,0 +1,104 @@ +#include +#include + +#include "binary.h" + +static int assertionCount = 0; +static int errorCount = 0; + +static void +to_decimal_test(const long long input, const long expected) { + + long result = to_decimal(input); + + if (result != expected) { + errorCount++; + fprintf(stderr, "\033[31m"); + fprintf(stderr, " (✖) For the binary number `%lld` expected `%ld`, got `%ld`", + input, expected, result); + fprintf(stderr, "\n\033[0m"); + fprintf(stderr, "\n"); + } else + { + printf("\033[32m (✓) Test passed\033[0m\n"); + } + assertionCount++; +} + +static void +to_binary_test(const long input, const long long expected) { + + long long result = to_binary(input); + + if (result != expected) { + errorCount++; + fprintf(stderr, "\033[31m"); + fprintf(stderr, " (✖) For the decimal number `%ld` expected `%lld`, got `%lld`", + input, expected, result); + fprintf(stderr, "\n\033[0m"); + fprintf(stderr, "\n"); + } else + { + printf("\033[32m (✓) Test passed\033[0m\n"); + } + assertionCount++; +} + +static void +is_binary_test(const long long input, const bool expected) { + + bool result = is_binary(input); + + if (result != expected) { + errorCount++; + fprintf(stderr, "\033[31m"); + fprintf(stderr, " (✖) For the decimal number `%lld` expected `%d`, got `%d`", + input, expected, result); + fprintf(stderr, "\n\033[0m"); + fprintf(stderr, "\n"); + } else + { + printf("\033[32m (✓) Test passed\033[0m\n"); + } + assertionCount++; +} + +int +main() { + to_decimal_test(1001001, 73); + to_decimal_test(1, 1); + to_decimal_test(111, 7); + to_decimal_test(11, 3); + to_decimal_test(0, 0); + + to_binary_test(73, 1001001); + to_binary_test(1, 1); + to_binary_test(7, 111); + to_binary_test(3, 11); + to_binary_test(0, 0); + + is_binary_test(111, 1); + is_binary_test(3377, 0); + is_binary_test(10101010, 1); + is_binary_test(19, 0); + is_binary_test(77777, 0); + + // Log total errors. + printf("\n"); + + if (errorCount != 0) { + printf("\033[31m"); + printf("(✖) Failed on %d of %d assertions", errorCount, assertionCount); + printf("\033[0m"); + printf("\n"); + exit(EXIT_FAILURE); + } + + // Or, log total successes. + printf("\033[32m"); + printf("(✓) Passed %d assertions without errors", assertionCount); + printf("\033[0m"); + printf("\n"); + + return 0; +}