mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +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)
|
||||
{
|
||||
b[0] = '\0';
|
||||
|
||||
int z;
|
||||
for (z = 65536; z > 0; z >>= 1) {
|
||||
strcat(b, ((x & z) == z) ? "1" : "0");
|
||||
unsigned int i = 0x80000000, leading_zeros = 1;
|
||||
if (x == 0) {
|
||||
*b++ = '0';
|
||||
*b = '\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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue