Skip to content

Commit 6485916

Browse files
committed
init
0 parents  commit 6485916

11 files changed

+395
-0
lines changed

Diff for: .editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*.c]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

Diff for: .gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.DS_STORE
2+
*.o
3+
*.log
4+
*.gcov
5+
*.gcda
6+
*.gcno
7+
binary
8+
test
9+
example
10+
coverage

Diff for: .travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: c
2+
compiler:
3+
# - clang
4+
- gcc
5+
script: make run-test
6+
after_script: sudo pip install cpp-coveralls && make run-coverage && coveralls --exclude test.c
7+
8+
notifications:
9+
email: false

Diff for: Makefile

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
TEST = test.c binary.c
2+
EXAMPLE = example.c binary.c
3+
OBJ_TEST = $(TEST:.c=.o)
4+
OBJ_EXAMPLE = $(EXAMPLE:.c=.o)
5+
6+
CFLAGS = -D_GNU_SOURCE -std=c99
7+
8+
LFLAGS = -Wall -Wno-format-y2k -W -Wstrict-prototypes \
9+
-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
10+
-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
11+
-Wnested-externs -Wredundant-decls
12+
13+
COVFLAGS = -Wall -fprofile-arcs -ftest-coverage
14+
15+
test: $(OBJ_TEST)
16+
$(CC) $(OBJ_TEST) -o $@
17+
18+
example: $(OBJ_EXAMPLE)
19+
$(CC) $(OBJ_EXAMPLE) -o $@
20+
21+
coverage: $(OBJ_TEST)
22+
gcc $(COVFLAGS) $(TEST) -o $@
23+
24+
.SUFFIXES: .c .o
25+
.c.o:
26+
$(CC) $< $(CFLAGS) $(LFLAGS) -c -o $@
27+
28+
run-coverage: coverage
29+
./coverage && gcov binary
30+
31+
run-test: test
32+
./test
33+
34+
run-example: example
35+
./example
36+
37+
clean:
38+
rm -f coverage test example $(OBJ_TEST) $(OBJ_EXAMPLE) *.gc{ov,da,no}
39+
40+
.PHONY: clean run-coverage run-test

Diff for: binary.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// An small library to work with
3+
// binary numbers.
4+
//
5+
// binary.c
6+
//
7+
// MIT licensed.
8+
// Copyright (c) Abraham Hernandez <[email protected]>
9+
//
10+
11+
#include <stdbool.h>
12+
13+
bool
14+
is_binary (long long binary) {
15+
bool status = true;
16+
while(true) {
17+
if (binary == 0) break;
18+
else {
19+
int tmp = binary % 10;
20+
if(tmp > 1) {
21+
status = false;
22+
break;
23+
}
24+
binary = binary / 10;
25+
}
26+
}
27+
return status;
28+
}
29+
30+
long
31+
to_decimal (long long binary) {
32+
if(!is_binary(binary)) return -1;
33+
34+
int decimal = 0;
35+
int multiplier = 1;
36+
37+
while (binary != 0) {
38+
decimal += (binary % 10) * multiplier;
39+
binary /= 10;
40+
multiplier *= 2;
41+
}
42+
return decimal;
43+
}
44+
45+
long long to_binary(long number) {
46+
long long binary = 0;
47+
int remainder;
48+
int i = 1;
49+
50+
while (number != 0) {
51+
remainder = number % 2;
52+
number /= 2;
53+
binary += remainder * i;
54+
i *= 10;
55+
}
56+
return binary;
57+
}
58+
59+
// TODO:
60+
// add_binary()
61+
// substract_binary() ...

Diff for: binary.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef BINARY_H
2+
#define BINARY_H
3+
4+
//
5+
// binary.h
6+
//
7+
// MIT licensed.
8+
// Copyright (c) Abraham Hernandez <[email protected]>
9+
//
10+
11+
#define bool int
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
bool
18+
is_binary(long binary);
19+
20+
long
21+
to_decimal(long long binary);
22+
23+
long long to_binary(long number);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
29+
#endif // BINARY_H

Diff for: example.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
#include "binary.h"
3+
4+
int main() {
5+
printf("%d\n", is_binary(1010));
6+
printf("%ld\n", to_decimal(10000000011));
7+
printf("%lld\n", to_binary(1000));
8+
}

Diff for: license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Abraham Hernandez <[email protected]> (abranhe.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "binary.c",
3+
"version": "1.0.0",
4+
"description": "An small library to work with binary numbers",
5+
"license": "MIT",
6+
"keywords": [
7+
"binary", "binary-numbers"
8+
],
9+
"repo": "abranhe/binary.c",
10+
"src": [
11+
"binary.h",
12+
"binary.c"
13+
]
14+
}

Diff for: readme.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<p align="center">
2+
<br>
3+
<img src="https://cdn.abranhe.com/projects/binary/logo.svg">
4+
<br>
5+
<br>
6+
<b>binary.c</b>: An small C library to work with binary numbers
7+
<br>
8+
</p>
9+
10+
<p align="center">
11+
<a href="https://travis-ci.org/abranhe/binary.c"><img src="https://img.shields.io/travis/abranhe/binary.c.svg?logo=travis" /></a>
12+
<a href="https://github.com/abranhe"><img src="https://abranhe.com/badge.svg"></a>
13+
<a href="https://cash.me/$abranhe"><img src="https://cdn.abranhe.com/badges/cash-me.svg"></a>
14+
<a href="https://patreon.com/abranhe"><img src="https://cdn.abranhe.com/badges/patreon.svg" /></a>
15+
<a href="https://github.com/abranhe/binary.c/blob/master/license"><img src="https://img.shields.io/github/license/abranhe/binary.c.svg" /></a>
16+
17+
<br>
18+
<br>
19+
</p>
20+
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.
21+
22+
23+
## Installation
24+
25+
*Installing using [Clib](https://github.com/clibs/clib)*
26+
27+
```sh
28+
$ clib install abranhe/binary.c
29+
```
30+
31+
## Usage
32+
33+
```c
34+
#include <stdio.h>
35+
#include "binary.h"
36+
37+
int main() {
38+
printf("%d\n", is_binary(1010));
39+
// 1
40+
printf("%ld\n", to_decimal(10000000011));
41+
// 1027
42+
printf("%lld\n", to_binary(1000));
43+
// 1111101000
44+
}
45+
```
46+
47+
## API
48+
49+
#### `bool is_binary(long binary);`
50+
51+
*Returns true (`1`) if the number is binary, otherwise return false (`0`)*
52+
53+
###### Params:
54+
55+
- `binary`: long binary number
56+
57+
#### `long to_decimal(long long binary);`
58+
59+
*Returns a long decimal number from a binary number*
60+
61+
###### Params:
62+
63+
- `binary`: long binary number
64+
65+
#### `long long to_binary(long decimal);`
66+
67+
*Returns a binary number from a decimal number*
68+
69+
###### Params:
70+
71+
- `decimal`: a long decimal number
72+
73+
## Team
74+
75+
|[![Carlos Abraham Logo][abranhe-img]][abranhe]|
76+
| :-: |
77+
| [Carlos Abraham][abranhe] |
78+
79+
## License
80+
81+
[MIT][license] License © [Carlos Abraham][abranhe]
82+
83+
<!-------------------- Links ------------------------>
84+
[abranhe]: https://github.com/abranhe
85+
[abranhe-img]: https://avatars3.githubusercontent.com/u/21347264?s=50
86+
[license]: https://github.com/abranhe/binary.c/blob/master/license
87+
[example]: https://github.com/abranhe/binary.c/blob/master/example.c
88+
[travis-badge]: https://img.shields.io/travis/abranhe/binary.c.svg
89+
[travis-status]: https://travis-ci.org/abranhe/binary.c
90+
[coverage-badge]: https://img.shields.io/coveralls/abranhe/binary.c.svg
91+
[coverage-status]: https://coveralls.io/r/abranhe/binary.c?branch=master

0 commit comments

Comments
 (0)