0

I have to take an integer of 80 bits in argument, convert it on binary and then make some bit shifts operation on it. I use this snippet (which seems to work fine) to store the argument:

uint64_t n;
seed= strtol(argv[1], &p, 10);
printf("n:%" PRIu64 "\n", n);

Then, I'd like to use this function to convert it to binary:

uint64_t decimal_binary(uint64_t n)
{
    uint64_t rem, i=1;
    uint64_t binary=0;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
}

But then:

printf("n:%" PRIu64 "\n", n); /* works fine, even for n >= 1048576 */
printf("n:%" PRIu64 "\n", decimal_binary(n)); /* works only for n <= 1048575 */

I will need to use bit shift operator, so I need a solution which will work with <<.

Shan-x
  • 1,146
  • 6
  • 19
  • 44
  • 3
    80-bit input won't fit in `uint64_t` and 1048576 will be 21 digits when it is converted to binary and won't fit in 64-bit unsigned integer when the binary is represented in decimal. Consider dealing the data as strings. – MikeCAT Nov 26 '15 at 15:06
  • Operator overload is useful in C++, but this seems too difficult task for C for me... – MikeCAT Nov 26 '15 at 15:07
  • I thought of using char array, but then convert it to binary was beyong my skills… – Shan-x Nov 26 '15 at 15:13

2 Answers2

1

8 bytes integer number not enough to store the binary format of a 80 bits number. Please consider to use a char array.

Example if the number is 3, the binary format of it is 11( 2 digits). If the number is 255, the binary number is 11111111 (8 digit). So i think when the number is larger than 1048575, you must use more than 21 digits to store it. It larger than max of 8 bytes number.

Viet
  • 553
  • 1
  • 4
  • 18
  • Yeah, but then how can I easily convert it in binary and make bit shifts ? – Shan-x Nov 26 '15 at 15:39
  • Give me a clearly example! Ex: if n is 255, the binary number is 11111111, which operator after? You want to use << operator with number 11111111? – Viet Nov 26 '15 at 15:43
  • 1
    The int has 80 bits. For exemple, considering 1234567890, I would have to convert it in binary, then make a bit shift and replace some specific bits of the binary number. – Shan-x Nov 26 '15 at 15:49
  • 80 bits number, it mean you must use a 80 digits number to store the binary format of it. And i think there is no type of variable in c can do this. Please consider that you should store it as char array, create new function to resolve << operator, and an other function can convert from char array to interger. – Viet Nov 26 '15 at 15:57
1

Your decimal_binary could take in a struct wrapped uint8_t [10] array (if you want to omit some bounds checking with pointers) and a pointer to output buffer. Arrange the 80-bit integer into the array as MSB, then loop through each byte bit by bit and write '0' or '1' to output, e.g.

typedef struct { uint8_t arr[10]; } S_big_integer_container;

void decimal_binary( S_big_integer_container bigUInt, char *output[81] )
{
    uint32_t i, j;
    uint32_t iOut = 0u;
    for ( i=0u; i<10u; i++ )
    {
        for ( j=0u; j<8u; j++)
        {
            char o = ( bigUInt.arr[i] & (0x80u >> j) ) ? '1' : '0';
            (*output)[iOut++] = o;
        }
    }
    (*output)[80] = '\0';
}

Now you have a binary string of 80 digits in the output array, which is easy to print. Some modification required if you want to omit leading zeros. Example here

sendaran
  • 566
  • 3
  • 9