diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-08-08 09:59:53 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-08-08 09:59:53 -0400 |
commit | 870bc006c145aa96e0c80d5e983b52286335b8f2 (patch) | |
tree | 557474d3673d883e4000b20273a229d3e8a196b4 | |
parent | ae880f1d803dd2bf4473849c4ce3860e398a4aa8 (diff) |
split declarations and definitions
-rw-r--r-- | binary.c | 24 | ||||
-rw-r--r-- | binary.h | 22 |
2 files changed, 26 insertions, 20 deletions
diff --git a/binary.c b/binary.c new file mode 100644 index 0000000..bba7dd0 --- /dev/null +++ b/binary.c @@ -0,0 +1,24 @@ +#include <stdint.h> +#include <string.h> + +#include "binary.h" + +uintmax_t binary(const char *s) +{ + char array[BINSTRLEN]; + memset(array, '0', sizeof(array)); + strcpy(array + (sizeof(array) - strlen(s)) - 1, s); + return BIN_FROM_ARRAY(array); +} + +char *binstr(size_t n, char s[n], uintmax_t b) +{ + memset(s, '0', n); + s[n - 1] = '\0'; + for (size_t i = 0; i < n - 2 && i < BIN_UINTMAXBITS; i++) { + if (b & ((uintmax_t)1 << i)) { + s[n - (i + 2)] = '1'; + } + } + return s; +} @@ -1,5 +1,4 @@ #include <limits.h> -#include <string.h> #include <stdint.h> #define BIN_UINTMAXBITS (sizeof(uintmax_t) * CHAR_BIT) @@ -34,22 +33,5 @@ #define BINSTRLEN (BIN_UINTMAXBITS + 1) #define BIN(_n) (BIN_FROM_ARRAY(#_n)) -static uintmax_t binfromstr(const char *s) -{ - char array[BINSTRLEN]; - memset(array, '0', sizeof(array)); - strcpy(array + (sizeof(array) - strlen(s)) - 1, s); - return BIN_FROM_ARRAY(array); -} - -static char *binstr(size_t n, char s[n], uintmax_t b) -{ - memset(s, '0', n); - s[n - 1] = '\0'; - for (size_t i = 0; i < n - 2 && i < BIN_UINTMAXBITS; i++) { - if (b & ((uintmax_t)1 << i)) { - s[n - (i + 2)] = '1'; - } - } - return s; -} +uintmax_t binary(const char *s); +char *binstr(size_t n, char s[n], uintmax_t b); |