mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 20:45:06 +02:00
Do not include leading 0's in binary strings
This commit is contained in:
parent
5c75f550fd
commit
218902038f
1 changed files with 15 additions and 5 deletions
20
runtime.c
20
runtime.c
|
@ -1373,13 +1373,23 @@ object Cyc_length(void *data, object l)
|
||||||
|
|
||||||
char *int_to_binary(char *b, int x)
|
char *int_to_binary(char *b, int x)
|
||||||
{
|
{
|
||||||
b[0] = '\0';
|
unsigned int i = 0x80000000, leading_zeros = 1;
|
||||||
|
if (x == 0) {
|
||||||
int z;
|
*b++ = '0';
|
||||||
for (z = 65536; z > 0; z >>= 1) {
|
*b = '\0';
|
||||||
strcat(b, ((x & z) == z) ? "1" : "0");
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (i){
|
||||||
|
if (x & i) {
|
||||||
|
*b++ = '1';
|
||||||
|
leading_zeros = 0;
|
||||||
|
} else if (!leading_zeros) {
|
||||||
|
*b++ = '0';
|
||||||
|
}
|
||||||
|
i >>= 1;
|
||||||
|
}
|
||||||
|
*b = '\0';
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue