diff options
author | pommicket <pommicket@gmail.com> | 2023-03-23 16:18:38 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-03-23 16:18:38 -0400 |
commit | af61b9be6a746421a0417d282134491213f2c03f (patch) | |
tree | b81b5fe297cc2d4f029ec111b9ac6a9228b1c4f8 /util.c | |
parent | 6643ff4c14cc27bf80189aadaf65c36ac2e7307f (diff) |
handle various :increment-number edge cases and binary
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -258,6 +258,28 @@ char *a_sprintf(const char *fmt, ...) { return str; } +void str_binary_number(char s[65], u64 n) { + if (n == 0) { + strcpy(s, "0"); + return; + } + + u64 digits = 0; + u64 m = n; + while (m) { + m >>= 1; + digits += 1; + } + + m = n; + s[digits] = '\0'; + char *p = s + digits - 1; + while (m) { + *p-- = (m & 1) + '0'; + m >>= 1; + } +} + // advances str to the start of the next UTF8 character static void utf8_next_char_const(const char **str) { |