-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6485916
Showing
11 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.DS_STORE | ||
*.o | ||
*.log | ||
*.gcov | ||
*.gcda | ||
*.gcno | ||
binary | ||
test | ||
example | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// An small library to work with | ||
// binary numbers. | ||
// | ||
// binary.c | ||
// | ||
// MIT licensed. | ||
// Copyright (c) Abraham Hernandez <[email protected]> | ||
// | ||
|
||
#include <stdbool.h> | ||
|
||
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() ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef BINARY_H | ||
#define BINARY_H | ||
|
||
// | ||
// binary.h | ||
// | ||
// MIT licensed. | ||
// Copyright (c) Abraham Hernandez <[email protected]> | ||
// | ||
|
||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <stdio.h> | ||
#include "binary.h" | ||
|
||
int main() { | ||
printf("%d\n", is_binary(1010)); | ||
printf("%ld\n", to_decimal(10000000011)); | ||
printf("%lld\n", to_binary(1000)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Abraham Hernandez <[email protected]> (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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<p align="center"> | ||
<br> | ||
<img src="https://cdn.abranhe.com/projects/binary/logo.svg"> | ||
<br> | ||
<br> | ||
<b>binary.c</b>: An small C library to work with binary numbers | ||
<br> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://travis-ci.org/abranhe/binary.c"><img src="https://img.shields.io/travis/abranhe/binary.c.svg?logo=travis" /></a> | ||
<a href="https://github.com/abranhe"><img src="https://abranhe.com/badge.svg"></a> | ||
<a href="https://cash.me/$abranhe"><img src="https://cdn.abranhe.com/badges/cash-me.svg"></a> | ||
<a href="https://patreon.com/abranhe"><img src="https://cdn.abranhe.com/badges/patreon.svg" /></a> | ||
<a href="https://github.com/abranhe/binary.c/blob/master/license"><img src="https://img.shields.io/github/license/abranhe/binary.c.svg" /></a> | ||
|
||
<br> | ||
<br> | ||
</p> | ||
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 <stdio.h> | ||
#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] | ||
|
||
<!-------------------- Links ------------------------> | ||
[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 |
Oops, something went wrong.