简体   繁体   中英

Binary Converter Not Working for Large Numbers

I'm writing C code that converts from an integer to its binary representation, reverses the binary and then converts back to an integer. It's working well, but I need it to work for input values up to 10^9. Right now, it seems to break for anything larger than 10^7. What I mean is I put in a number such as 10000000 and get out 17967657062982931584 as being the binary representation (despite being accurate for smaller integers). In addition, I am not sure where my int_to_binary is the only function experiencing this problem, or if the others need to be optimized for large inputs as well. Any ideas where I'm going wrong? Thank you.

#include <stdio.h>
#include <string.h>

unsigned long long int_to_binary(unsigned long long k) {
    if (k == 0) return 0;
    if (k == 1) return 1;
    return (k % 2) + 10 * int_to_binary(k/2);
}

unsigned long long reverse_int(unsigned long long l) {
        unsigned long long reversed; 
    reversed = 0;
        while (l > 0) {
                reversed = reversed * 10 + (l%10);
                l = l / 10;
        }
        return reversed;
}

unsigned long long binary_to_int(unsigned long long k) {
    char binaryString[80];
    snprintf(binaryString, 4, "%llu", k);
    unsigned long long decimal = 0;
    int length = strlen(binaryString);
    int i = 0;
    while(i < length) {
        decimal = (decimal << 1) + (binaryString[i] - 0x30);
        i++;
    }
    return decimal;
}

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);
    printf("You entered %d\n", number);
    unsigned long long b = int_to_binary(number);
    printf("In Binary, this is %llu\n", b);
    unsigned long long c = reverse_int(b);
    printf("When we reverse it, it looks like this: %llu\n", c);
    unsigned long long d = binary_to_int(c);
    printf("And when we convert that back to an int, it looks like this: %llu\n", d);
        return 0;
}

You're using long long to represent a binary number as a decimal. On many architectures this results in the maximum value being stored of 2**64-1 = 18446744073709551615.

If you put in a decimal number such as 10000000, it cannot be represented as a decimal-binary number in 64 bits.

The problem is attempting to manipulate a binary number as a sum of powers of ten. It's more efficient to manipulate binary directly. Just change 10 to 2 in the reverse_int function.

unsigned long long reverse_int(unsigned long long l) {
        unsigned long long reversed; 
        reversed = 0;
        while (l > 0) {
                reversed = reversed * 2 + (l%2);
                l = l / 2;
        }
        return reversed;
}

In order to print numbers in binary, write a loop, testing each bit in sequence, using modulo % or bitwise operators like & and >> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM