Improve dec_to_binary function.

This commit is contained in:
Enrico Fraccaroli
2021-12-15 14:10:35 +01:00
parent 69f3cc6b88
commit f53100453f
4 changed files with 10 additions and 8 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ const char *to_human_size(unsigned long bytes);
/// @brief Transforms the given value to a binary string.
/// @param value The decimal value.
/// @return String representing the binary value.
const char *dec_to_binary(unsigned long value);
const char *dec_to_binary(unsigned long value, unsigned length);
/// Prints a KERN_DEFAULT.
#define pr_default(...) dbg_printf(__FILENAME__, __func__, __LINE__, __DEBUG_HEADER__, KERN_DEFAULT __VA_ARGS__)
+4 -3
View File
@@ -12,6 +12,7 @@
#include "string.h"
#include "stdio.h"
#include "io/video.h"
#include "math.h"
/// Serial port for QEMU.
#define SERIAL_COM1 (0x03F8)
@@ -185,10 +186,10 @@ const char *to_human_size(unsigned long bytes)
return output;
}
const char *dec_to_binary(unsigned long value)
const char *dec_to_binary(unsigned long value, unsigned length)
{
static char buffer[33];
for (int i = 0; i < 32; ++i)
buffer[i] = bit_check(value, 31 - i) ? '1' : '0';
for (int i = 0, j = 32 - min(max(0, length), 32); j < 32; ++i, ++j)
buffer[i] = bit_check(value, 31 - j) ? '1' : '0';
return buffer;
}